15算法课程 111. Minimum Depth of Binary Tree
来源:互联网 发布:datagard 数据同步 编辑:程序博客网 时间:2024/06/05 19:56
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.
solution:
本题思路递归,为了求最小值,那么在递归返回时只要返回左右子树的的较小值即可,但是要注意特殊情况即存在左或右子树不存在的情况,此时递归返回值要返回左右子树较大的值。
code:
class Solution {public: int minDepth(TreeNode* root) { if(root==NULL) return 0; int leftDepth = minDepth(root->left); int rightDepth = minDepth(root->right); if(rightDepth==0||leftDepth==0) return max(leftDepth,rightDepth)+1; else return min(leftDepth,rightDepth)+1; }};
阅读全文
0 0
- 15算法课程 111. Minimum Depth of Binary Tree
- 15算法课程 104. Maximum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- [LeetCode]111.Minimum Depth of Binary Tree
- 【LeetCode】111.Minimum Depth of Binary Tree
- [Leetcode] 111. Minimum Depth of Binary Tree
- 111.Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【LeetCode】111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- osmdroid API解读(十三)
- jsp 判断标签使用
- java异步
- el表达式小于等于不等于小于等于
- Swift 之父 Chris Lattner 访谈录(超长完整版)
- 15算法课程 111. Minimum Depth of Binary Tree
- MVC、MVP、和MVVM
- 插入排序的变形
- web开发中常用css兼容代码(包括移动端)
- java(十三):volatile与内存模型
- HBase java client配置参数
- Python 核心编程习题2
- 页面动态删除js、css文件
- java关系视图