由买买提看人间百态

topics

全部话题 - 话题: leftheight
(共0页)
d*********g
发帖数: 154
1
来自主题: JobHunting版 - 亚麻三面面经,攒人品,求好运
第三题贴个刚写的java代码:
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
return helper(root) != -1;
}
private int helper(TreeNode root)
{
if(root == null) return 0;

int leftHeight = helper(root.left);
int rightHeight = helper(root.right);

if(leftHeight >= 0 && rightHeight >= 0 && Math.abs(leftHeight -
rightHeight) <= 1)
return Math.max(leftHeight, rightHeight)+1;
return -1;
}
c*****r
发帖数: 108
2
来自主题: JobHunting版 - 问一个leetcode上面binary tree的题目
最近打算开始做leetcode了。
做到determine a binary tree is balanced 这道题的时候,发现online judge可能少
考虑了一种情况。(另外这个题目在crack上面有,4th edition 2010版本上的答案明显
错的。新的版本我没看过。)
下面是我看到leetcode论坛上的一段code(是错的), 然而online judge pass了:
public class Solution {
public boolean isBalanced(TreeNode root) {
return height(root) != -1;
}
private int height(TreeNode root)
{
if(root == null)
return 0;
int leftHeight = height(root.left);
if(leftHeight == -1)
return -1;
... 阅读全帖
j******s
发帖数: 48
3
来自主题: JobHunting版 - 最近面的两道题,求解答
嗯,那应该是对于每一个节点(其实不需要)做Math.min(leftHeight,rightHeight)*2+
2?
(共0页)