Minimum Depth of Binary Tree
2013-06-03 12:29
387 查看
题目:
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.
代码如下:
int depth(TreeNode *root)
{
if(root==NULL)return 0;
int nleft=depth(root->left);
int nright=depth(root->right);
int max;
if(nleft==0)max=nright;
else if(nright==0)max=nleft;
else
{
max=nleft<nright?nleft:nright;
}
return max+1;
}
int minDepth(TreeNode *root) {
return depth(root);
}
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.
代码如下:
int depth(TreeNode *root)
{
if(root==NULL)return 0;
int nleft=depth(root->left);
int nright=depth(root->right);
int max;
if(nleft==0)max=nright;
else if(nright==0)max=nleft;
else
{
max=nleft<nright?nleft:nright;
}
return max+1;
}
int minDepth(TreeNode *root) {
return depth(root);
}
相关文章推荐
- 5.4.1 Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - BFS
- Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree(二叉树的最短深度)(BT、DFS)(*)
- <LeetCode><Easy> 111 Minimum Depth of Binary Tree
- 1.(树)Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- LeetCode Minimum Depth of Binary Tree
- Leetcode 111 Minimum Depth of Binary Tree
- 111 Minimum Depth of Binary Tree
- 【LeetCode】111 - Minimum Depth of Binary Tree
- leetcode-Minimum Depth of Binary Tree &&Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
- leetcode Minimum Depth of Binary Tree
- [LeetCode]---minimum-depth-of binary tree
- [LeetCode 111] Minimum Depth of Binary Tree
- minimum-depth-of-binary-tree(Leetcode)
- 【Leetcode】Minimum Depth of Binary Tree
- week18-leetcode #111-MinimumDepthofBinaryTree