【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
2014-11-29 15:56
477 查看
Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
与Maximum Depth of Binary Tree对照看
解法一:深度优先遍历
借助栈进行深度优先遍历(DFS),栈中存放的就是从根到当前节点的路径。
当遇到叶节点的时候,把当前栈中路径长度与minD比较,取较小值赋给minD。
返回minD即最小深度。
![](http://images.cnitblog.com/blog2015/458814/201504/051447569209725.jpg)
解法二:递归
注意:定义中需要到达叶节点,因此空的子节点不计算在内。
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
与Maximum Depth of Binary Tree对照看
解法一:深度优先遍历
借助栈进行深度优先遍历(DFS),栈中存放的就是从根到当前节点的路径。
当遇到叶节点的时候,把当前栈中路径长度与minD比较,取较小值赋给minD。
返回minD即最小深度。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int minDepth(TreeNode *root) { if(!root) return 0; stack<TreeNode*> stk; int ret = INT_MAX; unordered_map<TreeNode*, bool> visited; stk.push(root); visited[root] = true; while(!stk.empty()) { TreeNode* top = stk.top(); if(!top->left && !top->right) //leaf ret = min(ret, (int)stk.size()); if(top->left && visited[top->left] == false) { stk.push(top->left); visited[top->left] = true; continue; } if(top->right && visited[top->right] == false) { stk.push(top->right); visited[top->right] = true; continue; } stk.pop(); } return ret; } };
![](http://images.cnitblog.com/blog2015/458814/201504/051447569209725.jpg)
解法二:递归
注意:定义中需要到达叶节点,因此空的子节点不计算在内。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int minDepth(TreeNode *root) { if(!root) return 0; else if(root->left && !root->right) return 1+minDepth(root->left); else if(!root->left && root->right) return 1+minDepth(root->right); else return 1+min(minDepth(root->left), minDepth(root->right)); } };
![](http://images.cnitblog.com/blog2015/458814/201504/051426522176265.jpg)
相关文章推荐
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
- 【LeetCode】28. Implement strStr() (2 solutions)
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
- LeetCode Solutions : Remove Duplicates from Sorted List
- LeetCode 545. Boundary of Binary Tree
- 【Leetcode】543. Diameter of Binary Tree
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
- LeetCode Questions List (LeetCode 问题列表)- Java Solutions
- Leetcode-545. Boundary of Binary Tree
- 【LeetCode】46. Permutations (2 solutions)
- LeetCode - 111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- LeetCode 543. Diameter of Binary Tree
- [LeetCode]226. Invert Binary Tree
- Leetcode 111. Minimum Depth of Binary Tree
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
- 【LeetCode】137. Single Number II (3 solutions)
- 【LeetCode】104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 【LeetCode】69. Sqrt(x) (2 solutions)