[LeetCode]---minimum-depth-of binary tree
来源:互联网 发布:linux shell 等待命令 编辑:程序博客网 时间:2024/06/05 07:31
题目描述
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: //find the minimum depth int run(TreeNode *root) { int left=0; int right=0; if(root==NULL) return 0; if(root->left==NULL&&root->right==NULL) return 1; left=run(root->left); right=run(root->right); if(left==0&&right!=0) return right+1;//左子树为空而右子数不为空 if(left!=0&&right==0) return left+1;//右字树为空而左字数不为空 return left>right?right+1:left+1;//返回左右字树中高度较小的 }};
0 0
- LeetCode: Minimum Depth of Binary Tree
- [Leetcode] Minimum Depth of Binary Tree
- LeetCode : Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - BFS
- [LeetCode] Minimum Depth of Binary Tree - DFS
- 【leetcode】Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [LeetCode]Minimum Depth of Binary Tree
- [Leetcode]Minimum Depth of Binary Tree
- [leetcode]Minimum Depth of Binary Tree
- Leetcode: Minimum Depth of Binary Tree
- LeetCode-Minimum Depth of Binary Tree
- [leetcode] Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- 【Leetcode】Minimum Depth of Binary Tree
- LeetCode | Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- Leetcode 442. Find All Duplicates in an Array
- Java复习之异常处理
- JAVA文件与数据流(1)
- 蓝桥杯2013试题——振兴中华
- 线性表的链式存储结构
- [LeetCode]---minimum-depth-of binary tree
- 仿地图弹窗
- 【NOI1999】钉子和小球(BSOI1477)
- margin负值布局
- art emacs解决markdown-mode预览乱码
- JavaScript入门(二)
- [Python]实战——百度贴吧爬虫
- 2017
- PHP数据传输神器CURL详解