t******i 发帖数: 483 | 1 http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
getMinDepth(root, 0);
return min;
}
public static int min = Integer.MAX_VALUE;
public void getMinDepth(TreeNode node, int level) {
if (node == null) {
if (min > level)
min = level;
return;
} else {
getMinDepth(node.left, level + 1);
getMinDepth(node.right, level + 1);
}
}
}
Submission Result: Wrong Answer
Input: {0}
Output: 0
Expected: 1
请教一下为啥OJ的结果是0.. |
s******7 发帖数: 1758 | |
k*******a 发帖数: 433 | 3 Input: {0}
是什么意思呢?
是指空树吗? |
r*******2 发帖数: 104 | 4 应该是只有一个根结点,没有任何子结点,所以深度为1~
【在 k*******a 的大作中提到】 : Input: {0} : 是什么意思呢? : 是指空树吗?
|
h*d 发帖数: 19309 | 5 需要是叶结点才可以,如果根结点本身是叶结点,深度是1,如果一侧是空,需要计算
另外一侧到叶结点的深度
http://gongxuns.blogspot.com/2012/12/leetcode-minimum-depth-of-
【在 t******i 的大作中提到】 : http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ : /** : * Definition for binary tree : * public class TreeNode { : * int val; : * TreeNode left; : * TreeNode right; : * TreeNode(int x) { val = x; } : * } : */
|
k*******a 发帖数: 433 | 6 0应该表示根节点的值。为什么要去掉static? |
s******7 发帖数: 1758 | 7 好吧,我来解释一下为啥要去掉static
首先这个static就用得不恰当,min明显是instance of class 的返回值,根本不属于
class
他之所以得到0, 那是leetcode上一个test case 是{},返回为0, leetcode test应该
是用的同一个runtime, 没办法reset static variable导致的
而且他这个后面还有一个错误, 多算了 single child的情况 |
k*******a 发帖数: 433 | 8 Static的解释懂了。
貌似要比较左右子树的返回值才能确定当前最小深度。
【在 s******7 的大作中提到】 : 好吧,我来解释一下为啥要去掉static : 首先这个static就用得不恰当,min明显是instance of class 的返回值,根本不属于 : class : 他之所以得到0, 那是leetcode上一个test case 是{},返回为0, leetcode test应该 : 是用的同一个runtime, 没办法reset static variable导致的 : 而且他这个后面还有一个错误, 多算了 single child的情况
|
t******i 发帖数: 483 | 9 明白了,多谢指教!
【在 s******7 的大作中提到】 : 好吧,我来解释一下为啥要去掉static : 首先这个static就用得不恰当,min明显是instance of class 的返回值,根本不属于 : class : 他之所以得到0, 那是leetcode上一个test case 是{},返回为0, leetcode test应该 : 是用的同一个runtime, 没办法reset static variable导致的 : 而且他这个后面还有一个错误, 多算了 single child的情况
|
z****8 发帖数: 5023 | 10 原来这个是大牛的微博。。
【在 h*d 的大作中提到】 : 需要是叶结点才可以,如果根结点本身是叶结点,深度是1,如果一侧是空,需要计算 : 另外一侧到叶结点的深度 : http://gongxuns.blogspot.com/2012/12/leetcode-minimum-depth-of-
|