递归 - 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 [题目] 输入一棵二叉树的根结点,判断该树是不是平衡二叉树.如果某二叉 ...
随机推荐
- 真正的轻量级WebService框架——使用JAX-WS(JWS)发布WebService(转载)
WebService历来都很受重视,特别是Java阵营,WebService框架和技术层出不穷.知名的XFile(新的如CXF).Axis1.Axis2等. 而Sun公司也不甘落后,从早期的JAX-R ...
- 【学习总结】Git学习-GIT工作流-千峰教育(来自B站)
Git工作流指南 - av32575602 文档资料 目录: 1-什么是版本控制系统 2-工作流简介 3-集中式工作流 4-功能分支工作流 5-GitFlow工作流 小记: 初看差点放弃了,不过后面还 ...
- JAVA ==号和equals()的区别
==号和equals()方法都是比较是否相等的方法,那它们有什么区别和联系呢? 首先,==号在比较基本数据类型时比较的是值,而用==号比较两个对象时比较的是两个对象的地址值: int x = 10; ...
- Linux(Ubuntu)使用日记------vim复制内容到其他应用
1.用vim 打开一个文件,然后执行命令:reg 查看是否有 + 或者 × 号 或者执行:version 命令 查看是否有+clipboard 2.如果存在跳过此步骤.如果不存在:在终端输入 sud ...
- tensorflow函数/重要功能实现
一.基础函数 1.1 .tf.reduce_sum(input_tensor, axis) Computes the sum of elements across dimensions of a ...
- Ubuntu 14.04 mame sound fix
sudo vi '/etc/mame/mame.ini' samplerate 22050
- CF618G(利用浮点数精度+矩乘优化DP)
这题真的太神辣,%了一发题解,原来还能这么搞QWQ 设\(A_{i,j}\)表示不加任何限制时,第\(i\)个格子会出现权值为\(j\)的史莱姆的概率,则有: \[A_{i,j}=A_{i,j-1}* ...
- 重建docker实例
1.显示当前运行的docker实例: [root@docker-test /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORT ...
- 第二章 python的介绍及变量
1.编程语言的介绍 a.机器语言 使用二进制编写指令的编程方式 b.汇编语言 汇编指令与机器语言相对应 c.高级语言 需要借助特殊的工具将其转换成机器语言,但是方便人进行阅读理解的编程方式 从执行效率 ...
- java替换ascii表字符
如下: //处理特殊字符 public String dealSpecialXml(String xml){ String result = ""; //result = xml. ...