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 ...
随机推荐
- c# 通过解析mp3规范命名并上传服务器
引用using Shell32; private void Open_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = ne ...
- C++-传值与传引用的差别
//值传递与引用传递的差别 #include <iostream> #include <iomanip> using namespace std; void fiddle(in ...
- Access之C#连接Access
原文:Access之C#连接Access 如果是个人用的小程序的话.一般都推荐用Sqlite和Access 使用SQlite数据库需要安装SQLite驱动,详情:SQLite之C#连接SQLite 同 ...
- django 带參数的 url
url就像筋络一样把django这个大框架的各个部分紧紧的连接成一个总体,所以要了解django从url開始是一个不错的方向. 一般的view template url的关系这里就不讲了,以下会具体介 ...
- .net MVC AutoFac基地的环境建设
在Nuget在运行安装引用 Install-Package Autofac -Version 3.1.0 Install-Package Autofac.Mvc4 public static void ...
- add-apt-repository
也许你早已注意到,我们在介绍软件的时候,在安装这块一般都提供 PPA 源安装方式,这是一种快速方便的软件安装方法,但对于一些新手来说,对 PPA 并不是很熟悉,下面我们就详细介绍一下. # 我们先了解 ...
- 【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告
原题地址: https://oj.leetcode.com/problems/fraction-to-recurring-decimal/ 题目内容: Given two integers repre ...
- 基于最简单的FFmpeg采样读取内存读写:存储转
===================================================== 基于最简单的FFmpeg样品系列读写内存列表: 最简单的基于FFmpeg的内存读写的样例:内 ...
- LoadRunner获取一个独特的价值在执行的场景
/* * * 笔者:古柏涛 * 迄今:2015-2-26 * E-mail:gubotao@foxmail.com * * 内容概要: * 本代码产生一个从1970年1月1日0时開始累计以毫秒为单位的 ...
- OpenCV原则解读HAAR+Adaboost
因为人脸检测项目.用途OpenCV在旧分类中的训练效果.因此该检测方法中所使用的分类归纳.加上自己的一些理解.重印一些好文章记录. 文章http://www.61ic.com/Article/DaVi ...