93. 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

Example  1:
Input: tree = {1,2,3}
Output: true Explanation:
This is a balanced binary tree.
1
/ \
2 3 Example 2:
Input: tree = {3,9,20,#,#,15,7}
Output: true Explanation:
This is a balanced binary tree.
3
/ \
9 20
/ \
15 7 Example 3:
Input: tree = {1,#,2,3,4}
Output: false Explanation:
This is not a balanced tree.
The height of node 1's right sub-tree is 2 but left sub-tree is 0.
1
\
2
/ \
3 4
 
分治法:
/**
* 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) {
return maxDepth(root) != -1;
} public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int right = maxDepth(root.right);
int left = maxDepth(root.left);
if (right == -1 || left == -1 || Math.abs(right-left) > 1) {
return -1;
}
return Math.max(right,left) + 1;
}
}

Lintcode93-Balanced Binary Tree-Easy的更多相关文章

  1. 93. Balanced Binary Tree [easy]

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

  2. [leetcode] 110. Balanced Binary Tree (easy)

    原题链接 水题 深度搜索每一节点的左右深度,左右深度差大于1就返回false. class Solution { public: bool isBalanced(TreeNode *root) { b ...

  3. LeetCode_110. Balanced Binary Tree

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

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

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

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

  6. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  7. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  8. Leetcode 笔记 110 - Balanced Binary Tree

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

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

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

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

随机推荐

  1. tensorflow 基本内容

    tensorflow的结构 1.使用图(graphs)来表示计算任务 2.在被称之为会话(Session)的上下文(context)中执行图 3.使用tensor表示数据 4.通过变量(Variabl ...

  2. 来吧学学.Net Core之项目文件简介及配置文件与IOC的使用

    序言 在当前编程语言蓬勃发展与竞争的时期,对于我们.net从业者来说,.Net Core是风头正紧,势不可挡的.芸芸口水之中,不学习使用Core,你的圈内处境或许会渐渐的被边缘化.所以我们还是抽出一点 ...

  3. Go语言 切片长度和容量

    package main import "fmt" func main() { s := []int{2, 3, 5, 7, 11, 13} printSlice(s) // Sl ...

  4. ==还款-代偿(csv循环自动代偿)

    问题: 解决:传递参数错误,上一步就错了 问题:代偿返回这些信息 解决:传递参数错误,应该为${repayWay1},但是一直写的是${repayWay} 问题:如何从csv文件中逐条取项目编号,进行 ...

  5. namenode No valid image files

    1,角色日志报错 Encountered exception loading fsimage java.io.FileNotFoundException: No valid image files f ...

  6. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  7. 551.学生出勤记录I

    /* * @lc app=leetcode.cn id=551 lang=java * * [551] 学生出勤记录 I * * https://leetcode-cn.com/problems/st ...

  8. 2.第一个python 程序

    第一个python程序 一..python程序的编写步骤 1.创建  xxx.py文件(文件名不要中文) 文件名要以py为扩展名,因为导入的时候其他扩展名会报错.如果不导入的情况可以不限制扩展名. 2 ...

  9. Docker镜像配置redis集群

    redis版本:3.2.3 架构: 3节点redis集群,并为每个节点设置一个备用节点,共6个节点 1.安装redis镜像 docker load < docker.redis.tar.gz 2 ...

  10. 日志入库-log4j-mysql连接中断问题

    mysql5.6 druid1.0.17 log4j 1.2.16 一定时间后无法连接  CommunicationsException: Communications link failure 粗暴 ...