Minimum Depth of Binary Tree 二叉树的最小深度
2014-01-20 16:10
459 查看
/**
* 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==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else if(root->left&&root->right==NULL)
return minDepth(root->left)+1;
else if(root->right&&root->left==NULL)
return minDepth(root->right)+1;
else if(root->left&&root->right)
{
int left=minDepth(root->left);
int right=minDepth(root->right);
return (left<right?left:right)+1;
}
}
};
* 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==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else if(root->left&&root->right==NULL)
return minDepth(root->left)+1;
else if(root->right&&root->left==NULL)
return minDepth(root->right)+1;
else if(root->left&&root->right)
{
int left=minDepth(root->left);
int right=minDepth(root->right);
return (left<right?left:right)+1;
}
}
};
相关文章推荐
- 【LeetCode】Minimum Depth Of Binary Tree二叉树的最小深度 - Easy
- [Leetcode] The minimum depth of binary tree二叉树的最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Minimum Depth of Binary Tree(求二叉树的最小深度)
- lintcode minimum-depth-of-binary-tree 二叉树最小深度
- Minimum Depth of Binary Tree 二叉树最小深度
- LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)
- Minimum Depth of Binary Tree 求二叉树最小高度/深度@LeetCode
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- LeetCode OJ 之 Minimum Depth of Binary Tree(二叉树的最小深度)
- 求二叉树的最小深度 Minimum Depth of Binary Tree
- Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 [java]
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- [LeetCode-111] Minimum Depth of Binary Tree (二叉树最小深度)
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
- [LeetCode] Minimum Depth of Binary Tree 二叉树最小深度
- LeetCode:minimum-depth-of-binary-tree(二叉树最小深度)
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
- 【leetcode】Minimum Depth of Binary Tree 二叉树的最小深度 (JAVA)