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]:

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

Return true.

Example 2:

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

  1. 1
  2. / \
  3. 2 2
  4. / \
  5. 3 3
  6. / \
  7. 4 4

Return false.

  1. package leetcode.easy;
  2.  
  3. /**
  4. * Definition for a binary tree node. public class TreeNode { int val; TreeNode
  5. * left; TreeNode right; TreeNode(int x) { val = x; } }
  6. */
  7. public class BalancedBinaryTree {
  8. public boolean isBalanced(TreeNode root) {
  9. if (null == root) {
  10. return true;
  11. } else {
  12. return (Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1) && isBalanced(root.left)
  13. && isBalanced(root.right);
  14. }
  15. }
  16.  
  17. private static int maxDepth(TreeNode root) {
  18. if (null == root) {
  19. return 0;
  20. } else {
  21. return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
  22. }
  23. }
  24.  
  25. @org.junit.Test
  26. public void test1() {
  27. TreeNode tn11 = new TreeNode(3);
  28. TreeNode tn21 = new TreeNode(9);
  29. TreeNode tn22 = new TreeNode(20);
  30. TreeNode tn33 = new TreeNode(15);
  31. TreeNode tn34 = new TreeNode(7);
  32. tn11.left = tn21;
  33. tn11.right = tn22;
  34. tn21.left = null;
  35. tn21.right = null;
  36. tn22.left = tn33;
  37. tn22.right = tn34;
  38. tn33.left = null;
  39. tn33.right = null;
  40. tn34.left = null;
  41. tn34.right = null;
  42. System.out.print(isBalanced(tn11));
  43. }
  44.  
  45. @org.junit.Test
  46. public void test2() {
  47. TreeNode tn11 = new TreeNode(1);
  48. TreeNode tn21 = new TreeNode(2);
  49. TreeNode tn22 = new TreeNode(2);
  50. TreeNode tn31 = new TreeNode(3);
  51. TreeNode tn32 = new TreeNode(3);
  52. TreeNode tn41 = new TreeNode(4);
  53. TreeNode tn42 = new TreeNode(4);
  54. tn11.left = tn21;
  55. tn11.right = tn22;
  56. tn21.left = tn31;
  57. tn21.right = tn32;
  58. tn22.left = null;
  59. tn22.right = null;
  60. tn31.left = tn41;
  61. tn31.right = tn42;
  62. tn32.left = null;
  63. tn32.right = null;
  64. tn41.left = null;
  65. tn41.right = null;
  66. tn42.left = null;
  67. tn42.right = null;
  68. System.out.print(isBalanced(tn11));
  69. }
  70. }

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. flutter,flutter版本version/channel切换问题

    flutter go,官方的指南版本如下: 如何设置版本和channel,尝试 flutter help,发现原来flutter version不单是可以查所有版本(--version查当前版本)还可 ...

  2. (一)python3.7的安装

    1.从官网https://www.python.org/下载相应版本的安装包.一般下载 executable installer,x86 表示是 32 位的,x86-64 表示 64 位的. 2.可选 ...

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

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

  4. mac上安装Nginx详细教程

    1. 安装(可以用 brew 安装) sudo brew install nginx 2. 查看 nginx 版本 nginx -v 3. 启动 nginx sudo nginx 1也可以使用下面的命 ...

  5. c++之:new与malloc

    #include <iostream> using namespace std; void spacealloc_c() { //开辟内存空间---C语言风格 int *p = (int ...

  6. jenkins安装和国内镜像配置

    直接替换 $JENKINS_HOME/updates/default.json 成腾讯云的地址即可: JENKINS_HOME=xxxxxxxxx sed -i 's/http:\/\/updates ...

  7. DOM操作2

    一.API和WebAPI API就是接口,就是通道,负责一个程序和其他软件的沟通,本质是预先定义的函数. Web API是网络应用程序接口.包含了广泛的功能,网络应用通过API接口,可以实现存储服务. ...

  8. P2037 电话号码

    题目描述 一串由长长的数字组成的电话号码通常很难记忆.为了方便记忆,有种方法是用单词来方便记忆.例如用“Three Tens”来记忆电话3-10-10-10. 电话号码的标准形式是七位数字,中间用连字 ...

  9. iptables一些练习

    iptables 一些小练习 可以参考之前的一起食用 https://www.cnblogs.com/lovesKey/p/10909633.html 允许来自192.168.0.0/16网段的地址来 ...

  10. P1069 细胞分裂——数学题,质因数分解

    P1069 细胞分裂 我们求的就是(x^k)|(m1^m2) k的最小值: 先给m1分解质因数,再给每个细胞分解: 如果m1有的质因数,细胞没有就跳过: 否则就记录答案: 注意整数除法下取整的原则: ...