LeetCode_110. Balanced Binary Tree
110. 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.
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的更多相关文章
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- [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 ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 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 ...
- [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 ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree)/AVL树:
- [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 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
随机推荐
- flutter,flutter版本version/channel切换问题
flutter go,官方的指南版本如下: 如何设置版本和channel,尝试 flutter help,发现原来flutter version不单是可以查所有版本(--version查当前版本)还可 ...
- (一)python3.7的安装
1.从官网https://www.python.org/下载相应版本的安装包.一般下载 executable installer,x86 表示是 32 位的,x86-64 表示 64 位的. 2.可选 ...
- Java位运算总结:位运算用途广泛
前天几天研究了下JDK的Collection接口,本来准备接着研究Map接口,可是一查看HashMap类源码傻眼咯,到处是位运算实现,所以我觉得还是有必要先补补位运算知识,不然代码看起来有点费力.今天 ...
- mac上安装Nginx详细教程
1. 安装(可以用 brew 安装) sudo brew install nginx 2. 查看 nginx 版本 nginx -v 3. 启动 nginx sudo nginx 1也可以使用下面的命 ...
- c++之:new与malloc
#include <iostream> using namespace std; void spacealloc_c() { //开辟内存空间---C语言风格 int *p = (int ...
- jenkins安装和国内镜像配置
直接替换 $JENKINS_HOME/updates/default.json 成腾讯云的地址即可: JENKINS_HOME=xxxxxxxxx sed -i 's/http:\/\/updates ...
- DOM操作2
一.API和WebAPI API就是接口,就是通道,负责一个程序和其他软件的沟通,本质是预先定义的函数. Web API是网络应用程序接口.包含了广泛的功能,网络应用通过API接口,可以实现存储服务. ...
- P2037 电话号码
题目描述 一串由长长的数字组成的电话号码通常很难记忆.为了方便记忆,有种方法是用单词来方便记忆.例如用“Three Tens”来记忆电话3-10-10-10. 电话号码的标准形式是七位数字,中间用连字 ...
- iptables一些练习
iptables 一些小练习 可以参考之前的一起食用 https://www.cnblogs.com/lovesKey/p/10909633.html 允许来自192.168.0.0/16网段的地址来 ...
- P1069 细胞分裂——数学题,质因数分解
P1069 细胞分裂 我们求的就是(x^k)|(m1^m2) k的最小值: 先给m1分解质因数,再给每个细胞分解: 如果m1有的质因数,细胞没有就跳过: 否则就记录答案: 注意整数除法下取整的原则: ...