Minimum Depth of Binary Tree
来源:互联网 发布:ysbn免费网络 编辑:程序博客网 时间:2024/05/21 03:25
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.
class Solution {public: int minDepth(TreeNode *root) { if(root == NULL) return 0; int left = minDepth(root->left); int right = minDepth(root->right); if(!root->left || !root->right) return (left < right ? right : left) + 1; else return (left < right ? left : right) + 1; }};
结点的某个孩子结点为空的时候,那么经由该空孩子肯定到达不了叶子结点,那么只能是返回较大值了,当左右都可达时,那就返回较小值了。
0 0
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Action Result
- public protected private非常生动的解释
- VC6.0打开文件以及向工程中添加文件时程序崩溃自动退出
- 数据采集流程整理
- 元旦乐翻天,大奖拿不停--聊城大学元旦编程晚会纪实
- Minimum Depth of Binary Tree
- 白话经典算法系列之二 直接插入排序的三种实现
- VIM常用
- 白话经典算法系列之三 希尔排序的实现
- SolrJ提交索引
- java保留两位小数4种方法
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- Camera HAL overview
- 8086和80386寄存器总结