由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - [leetcode] Minimum Depth of Binary Tree 我的这个答案说wrong answer,但是我在本地跑就是对的.
相关主题
[leetcode] Maximum Depth of Binary TreeCareer cup 4.9 path sum的答案肯定错了
初始化binary tree请问一个简单的面试题
讨论一道LeetCode题:Binary Tree Maximum Path Sum弱问怎么判断两个binary tree相同?
Leetcode bst max path-----is this solution correct?Binary Tree Maximum Path Sum
问一个leetcode上面binary tree的题目弱问:leetcode里Convert Sorted List to Binary Search Tree
help: leetcode "Recover Binary Search Tree" -- 附代码check if a binary tree is a valid binary search tree
到底什么样的二叉树才是平衡二叉树?Binary Tree Maximum Path Sum
请教大家一个问题:Maximum Height (Depth) of a Binary Tree Using PreOrder TraversalFind the node with given value in binary tree in in-order
相关话题的讨论汇总
话题: treenode话题: public话题: int话题: level
进入JobHunting版参与讨论
1 (共1页)
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
2
去掉static就可以了
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-

1 (共1页)
进入JobHunting版参与讨论
相关主题
Find the node with given value in binary tree in in-order问一个leetcode上面binary tree的题目
leetcode Runtime error : Flatten Binary Tree to Linked Listhelp: leetcode "Recover Binary Search Tree" -- 附代码
Unique Binary Search Trees II的复杂度怎么算啊?多谢!到底什么样的二叉树才是平衡二叉树?
leetcode交了钱的能share一下题么?请教大家一个问题:Maximum Height (Depth) of a Binary Tree Using PreOrder Traversal
[leetcode] Maximum Depth of Binary TreeCareer cup 4.9 path sum的答案肯定错了
初始化binary tree请问一个简单的面试题
讨论一道LeetCode题:Binary Tree Maximum Path Sum弱问怎么判断两个binary tree相同?
Leetcode bst max path-----is this solution correct?Binary Tree Maximum Path Sum
相关话题的讨论汇总
话题: treenode话题: public话题: int话题: level