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.

做完了链表系列,这几天开始做树系列。对于解决关于树的问题,递归是一个很好的方法。对于这个题目我们也可以用递归来解决,给定一个节点,我们分别判断它的左子树是否平衡,右子树是否平衡。如果它的子树有不平衡的,那么该树是不平衡的。如果它的子树平衡,我们要比较一下两个子树深度差是否超过1。如果超过1,那么该树也是不平衡的。如何记录节点的深度呢,我们可以利用节点中的val来存储一个节点和它的子树组成的树的深度。是不是很巧妙呢?代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
if(root.left == null && root.right == null){
root.val = 1;
return true;
} int leftdeep = 0;
int rightdeep = 0;
if(root.left != null){
if(!isBalanced(root.left)) return false;
leftdeep = root.left.val;
}
if(root.right != null){
if(!isBalanced(root.right)) return false;
rightdeep = root.right.val;
} if(Math.abs(leftdeep - rightdeep) > 1) return false;
root.val = leftdeep>rightdeep?leftdeep+1:rightdeep+1;
return true;
}
}

LeetCode OJ 110. Balanced Binary Tree的更多相关文章

  1. Leetcode 笔记 110 - Balanced Binary Tree

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

  2. 【LeetCode OJ】Balanced Binary Tree

    Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...

  3. 【一天一道LeetCode】#110. Balanced Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】110. Balanced Binary Tree

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  5. LeetCode OJ:Balanced Binary Tree(平衡二叉树)

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

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

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

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

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

  8. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

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

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

随机推荐

  1. php +mysql 添加 删除 修改 insert into delete update

    INSERT INTO 插入数据库 $sql = "INSERT INTO subject (uid,fun,title) VALUES (3,88,'语文')"; $query ...

  2. Hadoop实践

    1.将HDFS中的文本文件读取并以JSON格式转存到MongoDB时,报磁盘不足的异常. 实验室的5台计算机的存储空间都在500G以上,就目前存储的数据量来看,完全达不到磁盘接近饱和的状态.通过查看H ...

  3. Hmaster启动后自己挂掉

    之前发现master中Hmaster进程自己挂掉,而worker中的HRegionServer进程关闭不掉. 最开始怀疑是hbase的pid文件存在tmp中,被系统自己删掉了,就在hbase文件中新建 ...

  4. 【HELLO WAKA】WAKA iOS客户端 之二 架构设计与实现篇

    上一篇主要做了MAKA APP的需求分析,功能结构分解,架构分析,API分析,API数据结构分析. 这篇主要讲如何从零做iOS应用架构. 全系列 [HELLO WAKA]WAKA iOS客户端 之一 ...

  5. python学习第一天内容整理

    .cnblogs_code { width: 500px } 一.python 的历史 (摘自百度百科,了解就ok) Python[1]  (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn ...

  6. 【转】MapReduce的优化

    相信每个程序员在编程时都会问自己两个问题“我如何完成这个任务”,以及“怎么能让程序运行得更快”.同样,MapReduce计算模型的多次优化也是为了更好地解答这两个问题. MapReduce计算模型的优 ...

  7. ARM驱动调试方法、思路总结、笔记

    驱动程序的调试一. 打印: prink, 自制proc文件UBOOT传入console=ttySAC0 console=tty11. 内核处理UBOOT传入的参数console_setup add_p ...

  8. iframe标签flash遮盖页面元素问题——wmode参数

    最近做项目过程中,遇到各种小问题,所以打算通过博客园来记录各问题的解决办法. 这篇的问题背景是这样子的:项目是用的AngularJS框架,在某个页面上用了wangEditor富文本编辑器插件(gith ...

  9. 这两天dede 仿站的收货

    首先学会了织梦的安装,其次学会了找织梦默认的模板目录在生成更新主页html和系统设置下的默认末班风格下,然后学会了写一些简单的循环输出代码,发现head2.htm没有找到,更新文件和缓存发现能用了

  10. poj_3261_Milk Patterns(后缀数组)

    题目链接:poj_3261_Milk Patterns 题意: 给你一串数,让你找重复出现不少于k次的子串的最长长度,重复出现可重叠 题解: 老套路,还是二分答案,然后用height数组来check答 ...