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. windows线程池四种情形(win核心读书笔记)

    windows线程池四种情形(win核心读书笔记) Mircosoft从Windows2000引入线程池API,并在Vista后对线程池重新构架,引入新的线程池API.以下所有线程池函数均适用于Vis ...

  2. form表单直接传文件

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  3. jquery选择器之基本筛选选择

    1.基本选择器 2.内容筛选选择器 3.可见性筛选选择器 4.属性筛选选择器 5.子元素筛选选择器 6.表单元素选择器 7.表单对象属性筛选器

  4. C#中ref和out的区别浅析

    这篇文章主要介绍了C#中ref和out的区别浅析,当一个方法需要返回多个值的时候,就需要用到ref和out,那么这两个方法区别在哪儿呢,需要的朋友可以参考下   在C#中通过使用方法来获取返回值时,通 ...

  5. UITextField(一)监听输入内容的变化(开发笔记)

    //添加事件UIControlEventEditingChanged [textField addTarget:self action:@selector(textFieldDidChange:) f ...

  6. iis虚拟目录或应用程序不继承父站点的web.config配置信息

    A为主站点 B为A的应用程序站点 再A的web.config中对不想继承的节点用location 套起来.如下: <location path="." allowOverri ...

  7. Shell出现cd命令无法识别

    出现cd 等命令无法识别的原因可能是: 当前文件实在windows环境下编辑的其换行结尾是 \r\n 和linux环境的 \n 不一致导致错误, 最好在linux系统上通过 VI 命令新建文件,然后通 ...

  8. 在 Sublime Text 3 中快捷打开 git-gui

    在文件夹右键菜单上可以方便的打开 git-gui,在 st 里面也可以添加这个右键菜单. 步骤如下: 安装 SideBarEnhancements 插件. 在 SideBarEnhancements ...

  9. 为啥使用Iscroll.js之后,a不能触发点击事件?

    原因:是iscroll.js阻止了a的行为. 解决方法:打开iscroll-probe.js,然后找到preventDefaultException方法. 然后修改为: // preventDefau ...

  10. UVa 10305 Ordering Tasks (例题 6-15)

    传送门: https://uva.onlinejudge.org/external/103/10305.pdf 拓扑排序(topological sort)简单题 自己代码的思路来自: ==>  ...