Minimum Depth of Binary Tree
2016-06-21 13:50
246 查看
二叉树的最小深度
View Code
同理可判断最大深度,因为是求最大值,所以无需判断该结点是否是叶子结点(如果不是叶子结点,肯定不是最大深度)。
int minDepth(TreeNode *root) { return minDepth(root, false); } int minDepth(TreeNode *root, bool hasbrothers) { if (root == nullptr)return hasbrothers ? INT_MAX : 0; return 1 + min(minDepth(root->left, root->right != nullptr), minDepth(root->right, root->left != nullptr)); }
View Code
同理可判断最大深度,因为是求最大值,所以无需判断该结点是否是叶子结点(如果不是叶子结点,肯定不是最大深度)。
相关文章推荐
- eventbus的使用(一)简洁易懂
- 【转】Unity中,使用Ngui 的默认字体Arial,显示出错;附带字体替换脚本
- 仿酷狗歌词的滚动效果
- MyEclipse2015 SSH对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决
- oracle数据泵备份恢复
- Fibonacci(自底向上)
- WORDPRESS商店主题:MUSIK音乐电台销售中文版(2.2.1)
- android反编译系列之--apktool的使用
- c# ip内外网判断
- Swift - 给表格的单元格UITableViewCell添加图片
- 【Android开发】经典的大牛博客推荐(排名不分先后)
- Linux 设备驱动开发思想 —— 驱动分层与驱动分离
- JAva使用DOM读取XML数据(解析)
- HDU 1075字典树(水题)
- DevExpress的TreeList开发示例
- Android存储数据
- windows MySQL5.7.9免安装版配置方法
- python多线程和多进程对比
- SAM格式 及 比对工具之 samtools 使用方法
- 第十一周项目点类派生直线类