Minimum Depth of Binary Tree LeetCode
来源:互联网 发布:诺维斯基10年数据 编辑:程序博客网 时间:2024/06/07 07:09
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int minDepth(TreeNode root) { if(root==null)return 0; else if(root.left==null||root.right==null) return 1+Math.max(minDepth(root.left), minDepth(root.right)); return 1+Math.min(minDepth(root.left), minDepth(root.right)); }}
递归思想要把一个问题分解成几个小问题的形式。在这个题目中,最短路径分为两种情况:
其一是该节点的左右子节点其中之一为空,此时以该节点为根的子树的最短路径为不为空的那个字结点为根的树的最短路径加一;
其二是两个子节点都不为空,此时此时以该节点为根的子树的最短路径为该节点两个子节点为根节点的树的最短路径的较小者加一。
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
- C++变量管理
- leetcode练习题目
- Using WinHTTP as a Side-by-side Assembly
- Strange SQLAlchemy error message: TypeError: 'dict' object does not support indexing
- UDP传输:简易聊天室的搭建。。。
- Minimum Depth of Binary Tree LeetCode
- 观察者设计模式
- switch语句case内声明对象
- List的contains方法
- mysql创建用户
- 我和opencv 2
- 好用的负margin
- 解决tabhost中的子Acitivity无法调用service的问题
- Mahout基于余弦相似度的评估