Balanced Binary Tree(Java代码没过,什么原因???)

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode *root) {
        int depth=0;
        return isBalanced(root,&depth);
    }
    bool isBalanced(TreeNode *root,int* depth){
        if(root==NULL){
            *depth=0;
            return true;
        }

        int left,right;
        if(isBalanced(root->left,&left) && isBalanced(root->right,&right)){
            int diff=left-right;
            if(diff<=1 && diff>=-1){
                *depth=1+(left>right?left:right);
                return true;
            }
        }
        return false;
    }

};

同样思路的Java代码没过,谁能告诉我问题所在,因为参数的问题吗?

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        int depth=0;
        return isBalanced(root,depth);
    }
    public boolean isBalanced(TreeNode node,int depth){
        if(node == null){
            return true;
        }
        int leftDepth=0,rightDepth=0;
        if( isBalanced(node.left,leftDepth) && isBalanced(node.right,rightDepth) ){
            int diff=leftDepth-rightDepth;
            if(diff<=1 && diff>=-1){
                depth=1+((leftDepth>rightDepth)?(leftDepth):(rightDepth));
                return true;
            }
        }
        return false;
    }
}

Balanced Binary Tree(Java代码没过,什么原因???)

时间: 2024-10-25 05:11:59

Balanced Binary Tree(Java代码没过,什么原因???)的相关文章

110. Balanced Binary Tree Java Solutions

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Subscribe to see which companies as

leetcode 110 Balanced Binary Tree ----- java

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 判断一棵树是否是平衡二叉树 利用高度的答案,递归求解. /** * D

Balanced Binary Tree leetcode java

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 题解: 采用递归的方法,要记录depth用来比较. 代码如下:

[LeetCode][Java] Balanced Binary Tree

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 题意: 给定一棵二叉树,判定它是否为平衡二叉树. 算法分析:

[Leetcode][JAVA] Minimum Depth of Binary Tree &amp;&amp; Balanced Binary Tree

Minimum Depth of Binary Tree 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. 比较左子树和右子树的深度,取小的那个返回上来,并+1. 需要注意的是如果没有左子树或右子树.那么无条件取存在的那一边子树深

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

LeetCode: Balanced Binary Tree 解题报告

Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Show Tags SOLU

110.Balanced Binary Tree Leetcode解题笔记

110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 很早以前做的了  准

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h