leetcode110
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int height(TreeNode node)
{
if (node == null)
{
return ;
}
int lH = height(node.left);
if (lH == -)
{
return -;
}
int rH = height(node.right);
if (rH == -)
{
return -;
}
if (lH - rH < - || lH - rH > )
{
return -;
}
return Math.Max(lH, rH) + ;
} public bool IsBalanced(TreeNode root)
{
if (root == null)
{
return true;
}
return height(root) != -; }
}
https://leetcode.com/problems/balanced-binary-tree/#/description
补充一个python的实现:
class Solution:
def __init__(self):
self.result = True def maxDepth(self,root):
if root == None:
return
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
d = max(l,r) +
if abs(l - r) > :
self.result = False
return d def isBalanced(self, root: TreeNode) -> bool:
self.maxDepth(root)
return self.result
leetcode110的更多相关文章
- leetcode-110:判断平衡二叉树 Java
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode110.平衡二叉树
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / \ 9 20 / \ 15 7 返回 true . 示例 ...
- LeetCode110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- leetcode110:combination-sum-ii
题目描述 给出一组候选数C和一个目标数T,找出候选数中起来和等于T的所有组合. C中的每个数字在一个组合中只能使用一次. 注意: 题目中所有的数字(包括目标数T)都是正整数 组合中的数字 (a 1, ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- Android Studio 中实现高德定位并获取相应信息
Android开发项目时常常会遇到定位这个功能,所以写了这篇博客,今天主要讲的高德地图的定位并获取相应信息. 首先导入高德的jar包 选中jar包右键点击 Add As Library, 在buil ...
- js获取当前日期(年月日格式)
function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var year = date ...
- n人围圈报数,报3出圈
题目:有n个人围成一圈,顺序排号.从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位. Scanner scanner = new Scanner(System. ...
- oracle非空约束
ALTER TABLE TB_ZJGL_DWSB_GRMX_LOG MODIFY HJQX NULL;
- VirtualBox使用物理硬盘建立磁盘
VirtualBox,只能用命令行来 建立磁盘才可以使用物理硬盘. 1.运行cmd,cd进入你的VirtualBox目录,如: cd C:\Program Files\Sun\VirtualBox ...
- js获取当前日期加上30天之后的日期
var date1 = new Date(); var date2 = new Date(date1); date2.setDate(date1.getDate() + 30); console.lo ...
- Mac iStat Menu 注册码
9185-4915-3252-3716-0000 1574-5977-7956-8062-0000 6015-5448-3282-4975-0000 9665-5955-6856-2071-0000 ...
- 爱奇艺、腾讯、优酷、搜狐、芒果、乐视、PPTV、音悦台等VIP视频免费观看
观看地址一:http://www.luoruiyuan.cn/pages/id-62_uid-2_btid-35.html 观看地址二:http://movie.luoruiyuan.cn/vip.h ...
- npm 与 package.json 快速入门
npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本文你将了解: 什么是 npm 安装 npm 更新 npm ...
- Eclipse中使用Maven,报错“$MAVEN_HOME”
1.今天在用eclipse时,执行maven命令,报错,如图"$MAVEN_HOME" 解决方案: 1.点击 windows---preferences,打开preferences ...