leetcode — balanced-binary-tree
/**
* 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的更多相关文章
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode——Balanced Binary Tree(判断是否平衡二叉树)
问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode - Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- [leetcode]Balanced Binary Tree @ Python
原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...
- leetcode -- Balanced Binary Tree TODO
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [Leetcode] Balanced binary tree平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- LeetCode——Balanced Binary Tree
Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-bal ...
- [LeetCode] Balanced Binary Tree 深度搜索
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 微信获取ticket及生成二维码(临时或永久)
微信获取ticket及生成二维码(临时或永久) curl.php---- define("APPID",""); define("APPSECRET& ...
- 安装xlwt和xlrd
因为想用python实现读写excel.百度了下,要安装xlwt和xlrd,网上各种方法,很多都不便利.最后利用pip安装很方便. 第一步:浏览器检索“xlwt安装”,点击第一个网页(百度) 即出现如 ...
- MongoDB 用Robomong可视化工具操作的 一些简单语句
一.数据更新 db.getCollection('表名').update({ "字段":{$in:["值"]} }, //更新条件 {$set:{ " ...
- JS与IOS、Android的交互
一.JS与Android 放在了assets文件夹下了(注意若使用的是AS这个IDE,assets文件夹应放在src/main目录下) <!DOCTYPE html> <html&g ...
- 方便快捷的求导求积分解方程在线工具sage介绍
有时候我们需要进行一些复杂的数学计算,比如求导, 求积分,解方程,还是用abcd字母代表变量的方程等,这就需要进行复杂的数学运算还需要具备良好的数学基础.不过现在有一个非常方便的在线工具,只需要几 ...
- MySql操作(一)
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- Echarts 几个常用图
最近公司业务上的 需求,要求做一些图表,我们技术框架上选择方便使用的Echarts. 下面是效果图: 下面是具体代码: <!DOCTYPE html> <html> <h ...
- 255.Spring Boot+Spring Security:使用md5加密
说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...
- emWin录音机,含uCOS-III和FreeRTOS两个版本
第12期:录音机配套例子:V6-921_STemWin提高篇实验_录音机(uCOS-III)V6-922_STemWin提高篇实验_录音机(FreeRTOS) 例程下载地址: http://forum ...
- js 生成随机炫彩背景
在浏览 https://ghost.org/xxxx/ 时. 可以使用 background-size: cover; 加上很小的像素图,放大后实现炫彩背景效果. 使用 js canvas 随机生成小 ...