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. Alibaba, I'm interested in you.

    Working for Alibaba is an aspiration for some. For other it’s the possibility of lucrative stock opt ...

  2. Linq中join多字段匹配

    错误示范: var projectSubmitInfos = (from project in db.T_PM_Project join member in db.T_PM_Member on pro ...

  3. zabbix基础服务搭建

    监控系统的介绍 性能优化和监控点 1.cpu           调度器          有效的分配cpu的时间片           上下文切换           运行队列            ...

  4. vue js实现获取两个日期之间所有日期

    https://blog.csdn.net/m0_37852904/article/details/85790793 // 计算续住的总日期列表 getAll(begin, end) { let ar ...

  5. Scala简介、安装、函数、面向对象

    Scala是一门以java虚拟机(JVM)为目标运行环境并将面向对象和函数式编程的最佳特性结合在一起的静态类型编程语言. scala命令行窗口,我们称之为REPL,是指:Read->Evalua ...

  6. UGUI背包系统

    在Unity3d中,UGUI提供了Scroll Rect.Grid Layout Group.Mask这三个组件,下面就给大家介绍下如何用这个三个组件来实现滚动视图. 首先放置好背包的背景图 在矩形线 ...

  7. docker容器实战-----初级<2>

    第二章  docker容器 1. Docker是通过内核虚拟化技术(namespaces及cgroups cpu.内存.磁盘io等)来提供容器的资源隔离与安全保障等.由于Docker通过操作系统层的虚 ...

  8. 关于动态添加iview admin路由以及刷新侧边栏

    在main.js中的mounted中使用router.addRouters()方法后界面上的路由并未刷新. 在注释掉路由跳转的权限限制之后,发现直接在地址栏输入对应路由能够进去,只是侧边栏没有刷新. ...

  9. struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)

    最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值. 1.在 web ...

  10. [批处理]使用IncrediBuilder加速自动化测试过程

    背景 1.目前单机全部跑完一次测试的时间大概是5个小时 2.7个虚拟机进行分布式执行的时间大概为3.5小时(包含复制.收集日志的时间) 预期 期望通过IncrediBuilder的虚拟进程的功能在单机 ...