/**
* 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. python学习:元组和嵌套

    tuple(元组):只是可读,不可以修改# tup1 = () #空元组# tup2 = (20,) #元组内有一个元素,需要在元素后添加逗号 a = (1,2,3,4)print(a[1])a[1] ...

  2. hashlib模块,shutil,模块 ,,xml 文件解析,configparser,模块,类,什么是类

    1 什么是hash hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值 如果把hash算法比喻为一座工厂 那传给hash算法的内容就是原材料 生成的hash值就是生产出的产品 2.为 ...

  3. 《JavaScript高级程序设计(第3版)》阅读总结记录第二章之在HTML中使用JavaScript

    本章目录: 2.1 <script> 元素 2.1.1 标签的位置 2.1.2 延迟脚本 2.1.3 异步脚本 2.1.4 在XHTML 中的用法 2.1.5 不推荐使用的语法 2.2 嵌 ...

  4. Vue-router重修01

    ---恢复内容开始--- 1.在vue中获取dom vue中不建议您亲自进行dom操作 vue实例内置ref属性存储或获取相应的dom元素 <div ref="dv"> ...

  5. Linux shell编程 -test

    test 命令的格式非常简单 test condition condition 是test命令要测试的一系列参数和值.当用在if-then 语句中时,test 命令看起来是这样的 if test co ...

  6. cadence电路板布线

    设置完约束规则后,便可以开始电路板的布线工作.

  7. 问题:这个新申请的内存为什么不能free掉?(已解决)

    一.问题描述 先上代码, /*** 省略 ***/ uChar *base64 = NULL; /*** 省略 ***/ base64 = (一段内存) /*** 省略 ***/ base64 = s ...

  8. CSS面试细节整理(一)

    最近面试时候发现自己最熟悉的css确实开发中好多细节没注意到,为了防止在栽跟头,打算从头到底捋一遍咯 语法部分: 1.css几种选择器的写法 (1)h1 em {color:red;} (后代选择器) ...

  9. js-day01-js语言基础

    JavaScript简介:JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本 ...

  10. jsp中input获得后台传递的值

    1.后台赋值 req.setAttribute("openId",openId); 2.前台获取值 value="<%= request.getAttribute( ...