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.
Given binary tree A = {3,9,20,#,#,15,7}
, B = {3,#,20,15,7}
A) 3 B) 3
/ \ \
9 20 20
/ \ / \
15 7 15 7
The binary tree A is a height-balanced binary tree, but B is not.
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
if (Math.abs(height(root.left) - height(root.right)) > ) return false;
return isBalanced(root.left) && isBalanced(root.right);
} public int height(TreeNode root) {
if (root == null) return ;
return Math.max(height(root.left), height(root.right)) + ;
}
}
public class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
return helper(root) != -;
} public int helper(TreeNode root) {
if (root == null) return ;
int leftHeight = helper(root.left);
int rightHeight = helper(root.right); if (leftHeight == - || rightHeight == -) return -;
if (Math.abs(leftHeight - rightHeight) > ) return -; return Math.max(helper(root.left), helper(root.right)) + ;
}
}
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= ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
随机推荐
- Ibatis学习总结2--SQL Map XML 配置文件
SQL Map 使用 XML 配置文件统一配置不同的属性,包括 DataSource 的详细配置信息, SQL Map 和其他可选属性,如线程管理等.以下是 SQL Map 配置文件的一个例子: Sq ...
- grunt使用watch和livereload的Gruntfile.js的配置
周末在家看angularJS, 用grunt的livereload的自动刷新, 搞了大半天, 现在把配置贴出来, 免得以后忘记了, 只要按照配置一步步弄是没有问题的; 开始的准备的环境安装是: (1) ...
- 在java类中,是先执行类的构造函数还是先执行类的私有非静态变量
举例子: package test_instance; public class TestClassLoaderTime { public TestClassLoaderTime(){ System. ...
- 【BZOJ-1452】Count 树状数组 套 树状数组
1452: [JSOI2009]Count Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1769 Solved: 1059[Submit][Stat ...
- The Reflection And Amplification Attacks && NTP Reply Flood Attack Based On NTP
目录 . NTP简介 . NTP协议格式 . NTP Reflect反射漏洞和NTP协议的关系 . 漏洞触发的前提和攻击需要的步骤 . 针对漏洞的攻防思考 1. NTP简介 Network Time ...
- TCP/IP Four Layer Protocol Format Learning
相关学习资料 tcp-ip详解卷1:协议.pdf 目录 . 引言 . 应用层 . 传输层 . 网络层 0. 引言 协议中的网络字节序问题 在学习协议格式之前,有一点必须明白,否则我们在观察抓包数据的时 ...
- Deformity PHP Webshell、Webshell Hidden Learning
目录 . 引言 . webshell原理介绍 . webshell的常见类型以及变种方法 . webshell的检测原理以及检测工具 . webshell隐藏反检测对抗手段 0. 引言 本文旨在研究W ...
- C#文件复制功能
目的是将用户自定义文件复制到指定文件夹并且能查看该文件,下面是个人做的源码: sing System; using System.Collections.Generic; using System.C ...
- c链表实现遇到的错误
想完成一个链表发现有错误,代码如下: //http://ac.jobdu.com/problem.php?pid=1511 //֮ǰÓÃlistʵÏֵģ¬½ñÌìÊÔÒ»ÏÂÓÃstruct ...
- cf340 C. Watering Flowers
C. Watering Flowers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...