【Leetcode】111. Minimum Depth of Binary Tree
来源:互联网 发布:软件与电子信息工程师 编辑:程序博客网 时间:2024/06/07 09:27
Description:
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) return 0; if(!root->left) return minDepth(root->right)+1; if(!root->right) return minDepth(root->left)+1; int lDepth = minDepth(root->left); int rDepth = minDepth(root->right); return min(lDepth,rDepth)+1; }};
阅读全文
0 0
- 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
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 【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
- 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.[Leetcode]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
- 关于SpringMVC,Web.xml监听器是否必须
- What is the difference between netty(io.netty.*) and jboss netty(org.jboss.netty.*)
- 调用face++ sdk实现人脸识别
- java多线程之synchronized
- NYOJ-7-街区最短路径问题
- 【Leetcode】111. Minimum Depth of Binary Tree
- Android中Context详解 ---- 你所不知道的Context
- linux下卸载samba组件
- MySQL压测sysbench/tpcc
- SOSOAPI前后端分离开发模式下的接口测试
- Python正则表达式
- 设计模式系列(12)创建者模式
- 图像旋转
- java 日期处理类【工具包系列】