/**
* Source : https://oj.leetcode.com/problems/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.
*/
public class BalancedBinaryTree { public boolean isBanlanced (TreeNode root) {
return treeDepth(root) == -1 ? false : true;
} /**
* 判断一棵二叉树是否是高度平衡二叉树
*
* 求出左右子树各自的总高度,如果不是则及早返回-1
*
* @param root
* @return
*/
public int treeDepth (TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = treeDepth(root.leftChild);
if (leftDepth == -1) {
return -1;
} int rightDepth = treeDepth(root.rightChild);
if (rightDepth == -1) {
return -1;
}
if (Math.abs(leftDepth-rightDepth) > 1) {
return -1;
}
return Math.max(leftDepth, rightDepth) + 1;
} public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() { }
} public static void main(String[] args) {
BalancedBinaryTree balancedBinaryTree = new BalancedBinaryTree();
char[] arr0 = new char[]{'#'};
char[] arr1 = new char[]{'3','9','2','#','#','1','7'};
char[] arr2 = new char[]{'3','9','2','#','#','1','7','5'};
System.out.println(balancedBinaryTree.isBanlanced(balancedBinaryTree.createTree(arr0)));
System.out.println(balancedBinaryTree.isBanlanced(balancedBinaryTree.createTree(arr1)));
System.out.println(balancedBinaryTree.isBanlanced(balancedBinaryTree.createTree(arr2)));
} }

leetcode — balanced-binary-tree的更多相关文章

  1. LeetCode: Balanced Binary Tree 解题报告

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

  2. [LeetCode] Balanced Binary Tree 平衡二叉树

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

  3. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

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

  4. LeetCode - Balanced Binary Tree

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

  5. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

  6. leetcode -- Balanced Binary Tree TODO

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

  7. [Leetcode] Balanced binary tree平衡二叉树

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

  8. LeetCode Balanced Binary Tree (判断平衡树)

    题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...

  9. LeetCode——Balanced Binary Tree

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

  10. [LeetCode] Balanced Binary Tree 深度搜索

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

随机推荐

  1. toString

    在java中使用toString: 如果在Java在输出定义一个Person类 然后实例化person  per 直接用system.out.println(per);无法得到我们想要的实例化内容 p ...

  2. 10-Python入门学习-函数的对象与嵌套、名称空间与作用域、闭包函数

    一.函数的对象 函数是第一类对象,指的是函数名指向的值(函数)可以被当作数据去使用 def func():# func=函数的内地址 print('from func') print(func) ag ...

  3. JavaScript中实现小数点后保留2位

    在项目中有时候会遇到要求输入的数字是整数或者小数点后绑定2位小数,因此可以用.toFixed(2)方法 下面是关于toFixed()方法的demo: <input type="numb ...

  4. JavaScript模板引擎Handlebars

    Handlebars模板库简单介绍 Handlebars是JavaScript一个语义模板库,通过对view(模板)和data(ajax请求的数据,一般是json)的分离来快速构建Web模板.它采用& ...

  5. mysql 语法积累

    1.把一个表中的某一列赋值到另一个表中的某一列 update sfa_token,sfa_member set sfa_token.mainid = sfa_member.mainid where s ...

  6. 3.jmeter接口测试---脚本录制

    安装好jmeter后,就要进入主题了,进行接口测试,接口测试的脚本获取方式 ①手动填写 ②badboy录制后,导入jmeter使用 ③jmeter录制 不会安装的可以进入这里:https://www. ...

  7. [AtCoder3856]Ice Rink Game - 模拟

    Problem Statement An adult game master and N children are playing a game on an ice rink. The game co ...

  8. 四、蛋炒饭(Egg fried rice)

    蛋炒饭,是一种常见菜肴.最早的记载见于1972年湖南长沙马王堆汉墓出土的竹简上有关"卵火高"的资料.经专家考证,"卵熇"是一种用黏米饭加鸡蛋制成的食品.有人推断 ...

  9. Windows下SQLMAP的安装图解

    第一步:BurpSuite扫描工具安装 //来源:http://www.mamicode.com/info-detail-563355.html Burp Suite 是用于渗透测试web 应用程序的 ...

  10. partial 的随笔

    partial class Dmeos { public int Ager { get; set; } public void Run() { Console.WriteLine(Ager); } } ...