【LeetCode练习题】Minimum Depth of Binary Tree
2014-04-13 16:31
399 查看
Minimum Depth of Binary Tree
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.
和上一题对应,求二叉树的最小深度。
解题思路:
参考上一题Maximun Depth of Binary Tree中最后那个极短的解法。
另外需要判断一下递归返回0的时候的结果不可取,因为不是叶节点。
代码如下:
class Solution { public: int minDepth(TreeNode *root) { if(!root) return 0; int l = minDepth(root->left); int r = minDepth(root->right); if(l * r != 0) return min(l,r)+1; else if(l == 0) return r+1; else return l+1; } };
相关文章推荐
- 杭电2036改革春风吹满地
- 添加第三方类库造成的linker command failed with exit code 1 (use -v to see invocation)的错误调试
- 简单计算器
- PostgreSQL 和Greenplum 区别
- 实验七_寻址方式在结构化数据中的应用
- ZooKeeper实践二
- Android某些浏览器下载的apk无法通过状态栏直接安装
- 字符串是否包含问题
- Android系统权限和root权限
- fdisk 添加新硬盘,划分硬盘分区,挂载流程
- web工程中各类地址的写法
- 线性代数学习笔记(十一)
- 上下文应用
- postgresql-9.3.2 tablefunc 安装
- 关于蓝牙接收数据的解决方案
- 第五(六)周作业 p1
- Wireless
- Android开发时提示Your project contains error(s),please fix them be
- android蓝牙开发
- drupal7里将分类属语表(taxonomy/term)生成菜单