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.

此题是经典的求二叉树深度深度题目,可采用递归的方式,以此求每个节点的左子树深度和右子树深度,然后返回该节点左右子树深度最大的那个即为该节点的深度

具体代码如下:

 /**
* 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 maxDepth(TreeNode *root) {
if(root == NULL)
return ; int nleft = maxDepth(root->left);
int nright = maxDepth(root->right); return (nleft > nright) ? (nleft+) : (nright+);
}
};

最新文章

  1. iTween 动画类型
  2. 使用MiniProfiler跟踪MVC + EF + Bootstrap 2 权限管理系统的性能消耗
  3. Step by Step
  4. could not open extension control file "/usr/share/postgresql/9.1/extension/plpythonu.control": No such file or directory
  5. 【Stage3D学习笔记续】山寨Starling(八):核心优化(批处理)的实现
  6. 【转】sun.misc.BASE64Encoder找不到jar包的解决方法
  7. $()和getElementById()的区别
  8. Lamada转化字符类型
  9. transient关键字小结
  10. Vue.js搭建路由报错 router.map is not a function,Cannot read property ‘component’ of undefined
  11. spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available
  12. 剑指Offer题解索引
  13. Qt 编程指南 4 单行编辑控件
  14. Github.Git
  15. js异步刷新局部页面
  16. C# ToShortDateString() ToString() 设置日期格式的区别
  17. 会话状态Session
  18. [Unity插件]Lua行为树(十三):装饰节点完善
  19. slf4j + log4j 需要的依赖
  20. 【mysql】sum处理null的结果

热门文章

  1. HBase 使用外部的 zookeeper
  2. 三、OPENERP 中的对象关系类型
  3. Mac休眠之后唤醒时无法使用鼠标
  4. javascript006_Object_模拟java的Map
  5. ACCESS 手工注入
  6. Dalvik与JVM区别
  7. nginx设置开机自启动
  8. emacs26.1 ppa
  9. 十分有趣的this指向题
  10. 08 Java 集合的线程安全问题