110. Balanced Binary Tree

Easy

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.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
/ \
9 20
/ \
15 7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
/ \
2 2
/ \
3 3
/ \
4 4

Return false.

package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class BalancedBinaryTree {
public boolean isBalanced(TreeNode root) {
if (null == root) {
return true;
} else {
return (Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1) && isBalanced(root.left)
&& isBalanced(root.right);
}
} private static int maxDepth(TreeNode root) {
if (null == root) {
return 0;
} else {
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
} @org.junit.Test
public void test1() {
TreeNode tn11 = new TreeNode(3);
TreeNode tn21 = new TreeNode(9);
TreeNode tn22 = new TreeNode(20);
TreeNode tn33 = new TreeNode(15);
TreeNode tn34 = new TreeNode(7);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = null;
tn21.right = null;
tn22.left = tn33;
tn22.right = tn34;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.print(isBalanced(tn11));
} @org.junit.Test
public void test2() {
TreeNode tn11 = new TreeNode(1);
TreeNode tn21 = new TreeNode(2);
TreeNode tn22 = new TreeNode(2);
TreeNode tn31 = new TreeNode(3);
TreeNode tn32 = new TreeNode(3);
TreeNode tn41 = new TreeNode(4);
TreeNode tn42 = new TreeNode(4);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = tn31;
tn21.right = tn32;
tn22.left = null;
tn22.right = null;
tn31.left = tn41;
tn31.right = tn42;
tn32.left = null;
tn32.right = null;
tn41.left = null;
tn41.right = null;
tn42.left = null;
tn42.right = null;
System.out.print(isBalanced(tn11));
}
}

LeetCode_110. Balanced Binary Tree的更多相关文章

  1. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  2. 110.Balanced Binary Tree Leetcode解题笔记

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

  3. [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 ...

  4. 【leetcode】Balanced Binary Tree(middle)

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

  5. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  6. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

  7. 平衡二叉树(Balanced Binary Tree)

    平衡二叉树(Balanced Binary Tree)/AVL树:

  8. [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 ...

  9. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

随机推荐

  1. IntelliJ IDEA lombok插件的安装和使用

    IntelliJ IDEA是一款非常优秀的集成开发工具,功能强大,而且插件众多.lombok是开源的代码生成库,是一款非常实用的小工具,我们在编辑实体类时可以通过lombok注解减少getter.se ...

  2. dos中查找端口的PID,并在任务管理器中处理端口

    本文来源https://www.cnblogs.com/lsyf/p/8979012.html 1.查看所有端口进程 首先点击开始菜单选择运行,接着在运行对话框中输入“cmd”,回车打开命令提示符窗口 ...

  3. idea远程debug调试阿里云ECS

    1.首先远程服务器的代码跟本地项目代码应该完全一致,否则会出现debug混乱现象,亲测. 2.config如图: ①命名可以省略②复制这个地址③输入远程ip和自定义且未被占用的端口号xxxx 3.开放 ...

  4. Java位运算总结:位运算用途广泛

    前天几天研究了下JDK的Collection接口,本来准备接着研究Map接口,可是一查看HashMap类源码傻眼咯,到处是位运算实现,所以我觉得还是有必要先补补位运算知识,不然代码看起来有点费力.今天 ...

  5. 使用bootstrap的栅格布局,用row后出现横向滚动条

    原因: **row默认有:margin-left:-15px; margin-right:-15px: 解决办法: **row外层需要包裹container或者container-fluid,一句话就 ...

  6. 题解 [51nod1358] 浮波那契

    题解 [51nod1358] 浮波那契 题面 解析 首先根据经验应该能一眼矩阵快速幂加速.... 因为给了你递推式,并且\(O(n)\)求显然不可能. 所以考虑怎么构造矩阵. 首先要处理的是小数的问题 ...

  7. 转,异常好的sql 基础知识整理

    转载自:http://blog.csdn.net/u011001084/article/details/51318434 最近从图书馆借了本介绍SQL的书,打算复习一下基本语法,记录一下笔记,整理一下 ...

  8. LOJ P10171 牧场的安排 题解

    每日一题 day6 打卡 Analysis 状压dp dp[i][j]+=dp[i-1][k]; #include<iostream> #include<cstdio> #in ...

  9. MongoDB 3.2变动一览

    3.2测试版本总算release了!E叔带大家来一览MongoDB 3.2版本的真容. (PS:内容比较多,在此仅针对个人认为比较重要的进行讲解,markdown写的,貌似WP的markdown插件有 ...

  10. Codeforces Global Round 4

    目录 Contest Info Solutions A. Prime Minister B. WOW Factor C. Tiles D. Prime Graph E. Archaeology F1. ...