Maximum Depth of Binary Tree
来源:互联网 发布:网络销售是什么工作 编辑:程序博客网 时间:2024/05/29 13:09
题目描述:
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
思路:二叉树求最大深度,最直接的方法就是递归。所求二叉树的最大深度等于它左、右子树中深度大的值+1,以此递归,递归边界值是root=NULL。
代码:
int maxDepth(TreeNode *root)
{
int depth = 1,l=0,r=0;
if(root==NULL)
return 0;
else
{
l = maxDepth(root->left);
r = maxDepth(root->right);
if(l>r) return depth+l;
else return depth+r;
}
}
0 0
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- 去掉List的重复对象(Arraylist)
- 黑马程序员——设计模式之单例设计模式
- lua 文件读写
- nm 参数详解
- solr搭建企业搜索平台,tomcat下
- Maximum Depth of Binary Tree
- Spring3 向数据库内插入数据出错
- Android sharedUserId研究记录
- adfasdf
- lame支持armv6 armv7 i386 armv7s arm64
- Android 用JDBC直连Mysql数据库
- toast提示控件使用(待写。。)
- Tomcat服务器配置
- solr搭建企业搜索平台,配置文件