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代码没有结束,是什么原因???)的更多相关文章
- leetcode 110 Balanced Binary Tree ----- java
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [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 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [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 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
随机推荐
- [置顶] 如何vs在cocos2dx项目中打印中文
一开始不是很理解,查了半天资料,终于找到解决方法,但是有部分中文还是不能打印出来,如 会出现部分的中文, 一开始都是问号的解决方法是 点击高级保存选项 设置成Unicode(UTF-8无签名) 这样就 ...
- CSS检测的高像素密度屏幕设备
iPhone4尽管是640px解析度,但它的屏幕宽度(device-width)目前只有320px和iPhone3G相同.只是iPhone4S的像素密度2. 然后使用meta viewport什么时候 ...
- UML相关工具一览
http://www.cnblogs.com/chehaoj/p/3478003.html TopCoder UML Tool 1.2.6 TopCoder, Inc http://www.topco ...
- SQL在declare声明变量
在sql添加的声明变量. declare @local_variable data_type 你需要指定一个变量声明的类型, 能够使用set和select对变量进行赋值, 在sql语句中就能够使用@l ...
- uva-442 Matrix Chain Multiplication
Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since ma ...
- win10无法新建文件夹怎么办 win10右键新建菜单设置方法
有朋友安装了win10系统后发现右键菜单中没有新建项,而平时使用新建 - 文件夹项的机率很高.如何才能恢复桌面右键菜单中的新建项呢? 右键点击桌面空白处,在右键菜单中发现没有新建项: 桌面右键菜单没有 ...
- iOS:编译错误 linker command failed with exit code 1 (use -v to see invocation)
将project不加入.m要求加入 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzI0MzQ2OQ==/font/5a6L5L2T/fontsi ...
- Could Not Connect
今天在写Quartz定时任务web应用时,访问项目页面出现Could Not Connect,后台也没报错. 然后我访问http://127.0.0.1:8080(Apache主页)的时候是正常的. ...
- J Dp
<span style="color:#000099;">/* ____________________________________________________ ...
- 这两个成员函数inline重新virtual种类
inlineType表示在编译时扩展功能,随着在函数调用的函数体而出替换函数调用:和vitual它是c++多态的必要条件,但为了表现出多态性,您将需要等到执行,要知道什么是真正的函数调用.从表面上看这 ...