【LeetCode】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.
【说明】
不是非常难,思路大家可能都会想到用递归,分别推断左右两棵子树是不是平衡二叉树,假设都是而且左右两颗子树的高度相差不超过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 解题报告的更多相关文章
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
随机推荐
- map、hash_map、unordered_map 的思考
#include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...
- Python Challenge 第十一关
第十一关,一张模糊的图,题目为 odd even,源代码中也没任何提示,看来又是图像处理. 这张模糊的图看起来没什么头绪,但是题目给了个奇数和偶数,就先试试坐标吧,根据原图来生成一个新图.我第一次尝试 ...
- JAVA基础之Set接口
个人理解: Set接口是Collection接口的子类,其继承了所有方法,HashSet集合则实现了Set接口,其内部存储数据时依靠哈希表,一个类似数组和链表的结合体.设置空集合时,存在默认的容量和加 ...
- linux grep 搜索查找
查找关键字在哪些文件夹中的哪些文件中出现(只列出文件名称): grep -l 15386257298 */* 查找关键字在哪些文件夹中的哪些文件中出现(列出文件名称+关键字): grep -o 153 ...
- hosts不支持泛解析
hosts不支持泛解析,只能是一个域名对应一个IP. 如果想要实现只能用一些第三方的DNS软件做解析.
- 调试SQLSERVER (一)生成dump文件的方法
http://www.cnblogs.com/lyhabc/p/4184149.html http://www.cnblogs.com/lyhabc/p/4185399.html
- iOS5可能会删除本地文件储存
文/ Nick (iphoneincubator) 关于iOS 5的本地文件储存Marco(Instapaper 的开发者)写过一篇很好的帖子阐述过相关问题,有兴趣的同学可以先阅读下他的文章然后再看下 ...
- C#面试基础知识2
1.C#三层架构 C#三层架构急表示层(UI,User Interface),业务逻辑层(BLL BusinessLogicLayer),数据访问层(DAL Data Access Layer).三层 ...
- useradd umask报错 root用su 切换到普通用户提示输入密码并报密码错误
添加新用户与以下文件有关联: /etc/default/useradd [root@localhost pam.d]# cat /etc/default/useradd # useradd defau ...
- php hex2bin 物联网设备发送十六进制数据
hex2bin("十六进制字符") 折腾了三天 后来大神一句话搞定 : hex2bin("十六进制字符") 网上有个方向是错误的 就是 "\xAA\x ...