递归 - Leetcode 110 判断二叉树是否为平衡二叉树
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 1:
Given the following tree [3,9,20,null,null,15,7]:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
解决方案(完美版):
class Solution {
public boolean isBalanced(TreeNode root) {
return helper(root) != -1;
}
public int helper(TreeNode root){
if(root == null) return 0;
int left = helper(root.left);
int right = helper(root.right);
if(left == -1 || right == -1 || Math.abs(left - right) > 1) return -1;
return Math.max(left, right) + 1;
}
}
自创版本:
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
return
(Math.abs(maxNodeLength(root.left) - maxNodeLength(root.right)) <= 1)
&& isBalanced(root.left)
&& isBalanced(root.right);
}
public int maxNodeLength(TreeNode root){
if(root == null){
return 0;
}
return 1 + Math.max(maxNodeLength(root.left),maxNodeLength(root.right));
}
}
递归 - Leetcode 110 判断二叉树是否为平衡二叉树的更多相关文章
- 【剑指offer】判断二叉树是否为平衡二叉树
2013-09-03 14:16:51 面试题39:求二叉树的深度.判断二叉树是否为平衡二叉树 小结: 根据平衡二叉树的定义,需要判断每个结点,因此,需要遍历二叉树的所有结点,并判断以当前结点为根的树 ...
- [Leetcode 100]判断二叉树相同 Same Tree
[题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- leetcode 98,判断二叉树为BST
方法一,记录子树的上界和下界,root的左子树一定小于root的值,root的右子树一定大于root的值,然后递归左子树和右子树 public class Solution { public bool ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...
- 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- leetcode 110
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 56. 2种方法判断二叉树是不是平衡二叉树[is balanced tree]
[本文链接] http://www.cnblogs.com/hellogiser/p/is-balanced-tree.html [题目] 输入一棵二叉树的根结点,判断该树是不是平衡二叉树.如果某二叉 ...
随机推荐
- Linux实战教学笔记50:Zabbix监控平台3.2.4(二)深入理解zabbix
https://www.cnblogs.com/chensiqiqi/p/9162986.html 一,Zabbix Web操作深入 1.1 Zabbix Web下的主机和模版以及监控项的添加方式 ( ...
- 基础数据类型:整型int、布尔值bool、字符串str、与for循环
1.整型 int() p2 long 长整型 p3 全部都是整型 2.布尔值 bool() True --- int() int(True) int() --- True bool(int) 注意点: ...
- AI AutoML
AutoML 参考链接: https://arxiv.org/pdf/1810.13306.pdf http://nooverfit.com/wp/7%E4%B8%AA%E4%BD%A0%E5%8F% ...
- Luogu P1852 BZOJ 2144 [国家集训队]跳跳棋
qwq 这题一看就不会,如果不是gg让做我是坚决不会做的 画图模拟,因为一次只能跳过一个棋子,所以对于一种情况只有三种移动方式: 中间向左跳 中间向右跳 左或右(距中间近的那个)向中间跳 发现,除了跳 ...
- Guava Cache探索及spring项目整合GuavaCache实例
背景 对于高频访问但是低频更新的数据我们一般会做缓存,尤其是在并发量比较高的业务里,原始的手段我们可以使用HashMap或者ConcurrentHashMap来存储. 这样没什么毛病,但是会面临一个问 ...
- 软件工程(GZSD2015) 第二次作业成绩
作业评分表 姓名 提交 语言 界面 PSP(3) CODE(4) 代码规范(2) 改进(1) 基本得分 提交时间 原始总得分 相对得分 最终得分 涂江凤 20150407 C CLI 3 4 2 1 ...
- 第二部分之AOF持久化(第十一章)
AOF持久化是通过保存Redis服务器所执行的写命令来记录数据库状态的.被写入AOF文件的所有命令都是以Redis的命令请求协议格式(纯文本)保存的. 一,AOF持久化的实现 1.命令追加 当AOF持 ...
- Callable,Future和FutureTask详解
1.Callable和Runnable 看Callable接口: public interface Callable<V> { /** * Computes a result, or th ...
- 修改host,上github
操作如下: 1.http://ping.chinaz.com/ 搜索github.com 海外ip,其实能找到的就两个;然后再搜gist.github.com 海外ip,也是两个. 192.30.25 ...
- PHP 5.6 中 Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version
解决方法 找到php.ini 文件, 把always_populate_raw_post_data 修改为-1 就行了. always_populate_raw_post_data=-1