算法 - 求二叉树的最大深度
来源:互联网 发布:网络兼职赚钱有哪些 编辑:程序博客网 时间:2024/04/30 12:57
CODE
<span style="font-size:14px;">public class Solution { public int maxDepth(TreeNode root) { if(root == null) return 0; int lMax = maxDepth(root.left); int rMax = maxDepth(root.right); return 1+((lMax>rMax)?lMax:rMax); }}</span>
核心思想
1 递归遍历所有节点
2 return 1+((lMax>rMax)?lMax:rMax);
(1) 1是指根结点
(2) 不能写成 return 1+ (lMax>rMax)?lMax:rMax; ,这里+运算符会影响三目运算符的正常使用,要养成勤加 ( ) 的习惯,避免一些未
知的危险。
return 1+ (lMax>rMax)?lMax:rMax;
0 0
- 算法 - 求二叉树的最大深度
- java 求二叉树最大深度算法
- 求二叉树的最大深度
- 求二叉树的最大深度
- 二叉树求最大深度
- 求二叉树的最大深度与最大宽度
- 求二叉树深度的算法
- 求二叉树的深度和宽度平衡最大距离
- leetcode 104求二叉树的最大深度
- Google算法题:二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- BZOJ 1213: [HNOI2004]高精度开根
- Thread详解8:wait/notify机制
- Android 提升效率利器(持续更新)
- 安装numpy、nltk问题汇总
- Node.js - RESTful API - web service
- 算法 - 求二叉树的最大深度
- vim自定义语法高亮
- RESTful Web Services: A Tutorial
- opengl常见问题
- Mysql 性能统计
- While executing gem ... (Gem::RemoteFetcher::FetchError)
- JAVA中this用法思考
- 08嵌入式—蔺小会—主要负责—2016BTV环球春晚
- Build a RESTful API Using Node and Express 4—Postman