【题目】

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。那么这棵树就是平衡二叉树。

【比較直观的Java代码】

/**
* Definition for binary tree
* 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){
return true;
}
if(Math.abs(depth(root.left)-depth(root.right)) > 1){
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
} public int depth(TreeNode root){
if(root==null){
return 0;
}
return 1+Math.max(depth(root.left), depth(root.right));
}
}

【改进后】

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
height(root);
return run(root);
} public boolean run(TreeNode root) {
if (root == null) return true; int l = 0, r = 0;
if (root.left != null) l = root.left.val;
if (root.right != null) r = root.right.val;
if (Math.abs(l - r) <= 1 && isBalanced(root.left) && isBalanced(root.right)) return true; return false;
} public int height(TreeNode root) {
if (root == null) return 0;
root.val = Math.max( height(root.left), height(root.right) ) + 1;
return root.val;
}
}

利用了TreeNode结构中的val,用它来记录以当前结点为根的子树的高度,避免多次计算。

【LeetCode】Balanced Binary Tree 解题报告的更多相关文章

  1. LeetCode: Balanced Binary Tree 解题报告

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

  2. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  3. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  4. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  5. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  6. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  8. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  9. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

随机推荐

  1. 解决IE6的PNG透明

    http://www.jb51.net/article/35669.htm http://blog.csdn.net/mosliang/article/details/6760028

  2. LeetCode OJ-- Jump Game II **

    https://oj.leetcode.com/problems/jump-game-ii/ 给一个数列,每次可以跳相应位置上的步数,问跳到最后位置至少用几步. 动态规划: j[pos]表示从0到po ...

  3. Codeforces 746G(构造)

                                                                                                      G. ...

  4. Codeforces 815 C Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  5. OnClick五种事件处理

    (一)内部类 1,布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  6. 在html里网页中嵌入优酷的视频

    <html> <embed src="http://player.youku.com/player.php/sid/XMjAzOTk4NjI4/v.swf" qu ...

  7. 区间最小值(2) (线段树 更新区间)2015年 JXNU_ACS 算法组暑假第一次周赛

    区间最小值(2) Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Subm ...

  8. C#面试基础题1

    1.简述 private. protected. public. internal 修饰符的访问权限.(C++中没有internal) private : 私有成员, 在类的内部才可以访问 ,也就是类 ...

  9. 【GLSL教程】(七)逐像素的光照 【转】

    http://blog.csdn.net/racehorse/article/details/6662540 逐像素的方向光(Directional Light per Pixel) 这一节将把前面的 ...

  10. Cookie常用方法封装Utils

    1.查询某个指定的cookie package com.sun.etalk.cookie; import javax.servlet.http.Cookie; public class CookieU ...