leetcode 111:Minimum Depth of Binary Tree
2017-05-27 10:29
435 查看
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.
class Solution {
public:
int minDepth(TreeNode* root) {
int ret=1000000;
if(!root) return 0;
help(root,1,ret);
return ret;
}
private:
void help(TreeNode* root,int depth,int& ret)
{
if(!root->left&&!root->right)
ret=min(depth,ret);
else if(root->left&&root->right)
{
help(root->left,depth+1,ret);
help(root->right,depth+1,ret);
}
else if(root->right&&!root->left)
help(root->right,depth+1,ret);
else
help(root->left,depth+1,ret);
}
int min(int a,int b)
{
return a<b?a:b;
}
};
呵呵哒,老子很失落!
学习下大佬的代码:BFS
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
queue<TreeNode*> Q;
Q.push(root);
int i = 0;
while (!Q.empty()) {
i++;
int k = Q.size();
for (int j=0; j<k; j++) {
TreeNode* rt = Q.front();
if (rt->left) Q.push(rt->left);
if (rt->right) Q.push(rt->right);
Q.pop();
if (rt->left==NULL && rt->right==NULL) return i;
}
}
return -1; //For the compiler thing. The code never runs here.
}
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
class Solution {
public:
int minDepth(TreeNode* root) {
int ret=1000000;
if(!root) return 0;
help(root,1,ret);
return ret;
}
private:
void help(TreeNode* root,int depth,int& ret)
{
if(!root->left&&!root->right)
ret=min(depth,ret);
else if(root->left&&root->right)
{
help(root->left,depth+1,ret);
help(root->right,depth+1,ret);
}
else if(root->right&&!root->left)
help(root->right,depth+1,ret);
else
help(root->left,depth+1,ret);
}
int min(int a,int b)
{
return a<b?a:b;
}
};
呵呵哒,老子很失落!
学习下大佬的代码:BFS
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
queue<TreeNode*> Q;
Q.push(root);
int i = 0;
while (!Q.empty()) {
i++;
int k = Q.size();
for (int j=0; j<k; j++) {
TreeNode* rt = Q.front();
if (rt->left) Q.push(rt->left);
if (rt->right) Q.push(rt->right);
Q.pop();
if (rt->left==NULL && rt->right==NULL) return i;
}
}
return -1; //For the compiler thing. The code never runs here.
}
相关文章推荐
- Leetcode#111 Minimum Depth of Binary Tree
- leetCode #111 Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree
- leetcode_111题——Minimum Depth of Binary Tree(二叉树,队列)
- [LeetCode]111 Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree [111]
- Java for LeetCode 111 Minimum Depth of Binary Tree
- Leetcode[111]-Minimum Depth of Binary Tree
- Leetcode 111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- leetcode_question_111 Minimum Depth of Binary Tree
- leetcode-111 Minimum Depth of Binary Tree
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
- LeetCode(111)Minimum Depth of Binary Tree
- Leetcode_111_Minimum Depth of Binary Tree
- [Leetcode 111, Easy] Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- leetcode_question_111 Minimum Depth of Binary Tree
- [leetcode-111]Minimum Depth of Binary Tree(C语言)
- 【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree