leetcode-111. Minimum Depth of Binary Tree
2016-05-01 13:24
429 查看
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.
Subscribe to see which companies asked this question
思路:递归
The minimum depth is the number of nodes along the shortest path from the root node down to >the nearest leaf node.
Subscribe to see which companies asked this question
思路:递归
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { int minLength = 1; bool stopFlag = false; public: int minDepth(TreeNode* root) { if(root == NULL) { return 0; } int leftDep = minDepth(root->left); int rightDep = minDepth(root->right); return (leftDep == 0 || rightDep == 0)?(leftDep+rightDep+1):min(leftDep,rightDep)+1; } };
相关文章推荐
- cocos2d-x pageView左右循环滑动
- 修改CentOS的IP地址
- 【链式栈】链式栈的封装<源代码>
- java.lang.Class.forName(String name, boolean initialize, ClassLoader loader)方法
- HDOJ5672
- 关于springmvc的流程
- 第 28 章 CSS3 多列布局
- uva 612 DNA Sorting
- 第十一周项目3.2 警察和厨师 为Polic类和Cook类增加了对象成员,请扩充代码
- Ubuntu14.04 jsoncpp搭建
- __attribute__ ((packed))
- HDUOJ Highways (最小生成树)
- SICP 换零钱的迭代版本
- HDUOJ1864最大报销额(01背包)
- java.lang.Class.forName(String name, boolean initialize, ClassLoader loader)方法
- html中音频和视频
- Android M 6.0 Build about 64-bit (__arm64__)
- SICP 关于递归迭代的重新理解以及尾递归的引入...
- 博客汇总
- 第十一周项目3.1 警察和厨师