[LeetCode]Minimum Depth of Binary Tree
来源:互联网 发布:java用户角色权限 编辑:程序博客网 时间:2024/05/23 00:32
题目描述
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.
二叉树的遍历。递归遍历左右子树,返回最短的路径长度。递归结束的条件是,如果根为空,则返回0.
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int run(TreeNode root) { if(root==null) return 0; if(root.left==null) return run(root.right)+1; if(root.right==null) return run(root.left)+1; return Math.min(run(root.left),run(root.right))+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
- GOF之观察者模式(2)
- python 字典(dict)按键和值排序
- spark常用函数:transformation和action
- 关于CGImageCreateWithImageInRect
- POJ1979 Red and Black
- [LeetCode]Minimum Depth of Binary Tree
- hdu 4764 Stone(巴什博弈)
- jzoj 1352. 【2011.12.24普及模拟】游荡的奶牛 解题报告
- 关于Mybatis的Batch模式性能测试及结论
- python 结巴分词(jieba)学习
- JAVA学习代码——单例模式
- 觉得对我这种随便起变量名的人有很大帮助的一篇文章
- GOF之观察者模式(一)
- PHP_Memcache函数详解