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= ...
随机推荐
- master-worker常驻型程序代码修改哪些需要重启master或者worker
之前在yii的项目里用redis作为消息队列,现在很多任务需要延迟需求,于是把之前redis的消息队列替换成了rabbitmq 于是使用yii的yii2-queue这个组件 但是由于提供的yii qu ...
- jQuery实现点击复制效果
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...
- Django REST framework+Vue 打造生鲜电商项目(笔记八)
(form:http://www.cnblogs.com/derek1184405959/p/8862569.html) 十一.pycharm 远程代码调试 第三方登录和支付,都需要有服务器才行(回调 ...
- MySql更改用户密码
1. use mysql show tables;查看mysql数据库中的表,会看到一个user表. select * from user;查看一下这个表中是否有root用户,如果有: update ...
- Mac配置Fiddler抓包工具
前言 一直以来都是在Win环境下工作,对于抓包工具,自然当属Fiddler最最出色,不过Fiddler是在.Net runtime环境下运行的,所以想要在Mac下使用,需要配置一些依赖.在试过了Cha ...
- [GraphQL] Query GraphQL Interface Types in GraphQL Playground
Interfaces are similar to Unions in that they provide a mechanism for dealing with different types o ...
- Java链接Redis时出现 “ERR Client sent AUTH, but no password is set”
Java链接Redis时出现 “ERR Client sent AUTH, but no password is set” 异常的原因及解决办法. [错误提示] redis.clients.jedis ...
- JSP数据交互(二)
Application:当前服务器(可以包含多个会话):当服务器启动后就会创建一个application对象,被所有用户共享page.request.session.application四个作用域对 ...
- HashMap的底层结构和原理
http://youzhixueyuan.com/the-underlying-structure-and-principle-of-hashmap.html HashMap是Java程序员使用频率最 ...
- P1143 进制转换
漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P1143 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入输出格式 输入格式: 输入数据共 ...