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代码没有结束,是什么原因???)的更多相关文章

  1. leetcode 110 Balanced Binary Tree ----- java

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

  2. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. 110.Balanced Binary Tree Leetcode解题笔记

    110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  5. 【leetcode】Balanced Binary Tree(middle)

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

  6. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  7. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  8. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  9. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  10. LeetCode: Balanced Binary Tree 解题报告

    Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...

随机推荐

  1. 依法使用Linux,反对Linux国产化

    10月28日.中国操作系统应用推广大会在京举行,包含中央办公厅.公安部.工信部在内的多个中央国家机关部委出席该会.此次推广大会的召开或标志着操作系统国产化进程的进一步推进. 会上.中国project院 ...

  2. 再说JNDI

    说到JNDI,即熟悉又陌生,熟悉在常常使用,如EJB3.0中的@EJB注入,底层实现即是JNDI的方式:喜闻乐见的: Context ctx=new InitialContext(); Object ...

  3. UVA 674 (入门DP, 14.07.09)

     Coin Change  Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...

  4. 【POJ1741】Tree 树分而治之 模板略?

    做广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog. ...

  5. FusionCharts简明教程(一)---建立FusionCharts图形

    由于该项目需要的报告需要做的事情,选择FusionCharts作为一种工具. 由于该报告没有任何接触,网上有没有更具体fusionCharts课程,所以我们决定做一个彻底的研究FusionCharts ...

  6. eclipse git 一个错误:the current branch is not configured for pull No value for key branch.xxx.merge found

    eclipse git 一个错误:the current branch is not configured for pull No value for key branch.xxx.merge fou ...

  7. Linux的proc文件系统

    proc,用户空间和内核空间能够通过该接口通信, 与普通文件不同的是.这些虚拟文件的内容都是动态创建的. proc文件系统是一个伪文件系统,它仅仅存在内存其中,而不占用外存空间. 它以文件系统的方式为 ...

  8. DevExpress 12.1 换肤 超级简单的方法(2013-11-5版)

    本例子是按照DevExpress 12.1 版本 进行演示.请先准备好DevExpress.BonusSkins.v12.1.dll 和DevExpress.Utils.v12.1.dll 1.首先添 ...

  9. 猫学习IOS(四)UI半小时就搞定Tom猫

    阿土 首先对影响 下载项目的源材料: Tom猫游戏代码iOS 素材http://blog.csdn.net/u013357243/article/details/44457357 效果图 以前风靡一时 ...

  10. c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串

    1 Encoding.Default.GetByteCount(checkString);  =2 全角 =1 半角 /// <summary> /// 获取字符串的字节长度 /// &l ...