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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- sql语句中处理金额,把分换算成元
问题,sql语句中直接将金额/100返回的结果会有多个小数位. as value from account as acc left join conCategory as cate on acc.ca ...
- HDU-4336-期望dp-bit
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ASP.NET ValidationSummary 控件
ASP.NET ValidationSummary 控件 Validation 服务器控件 定义和用法 ValidationSummary 控件用于在网页.消息框或在这两者中内联显示所有验证错误的摘要 ...
- 终于知道为什么我的mysql总是卸载的不干净以及老是找不到my.ini文件
感谢博主: http://blog.sina.com.cn/s/blog_6fc5bfa90100qmr9.html 如果你的电脑里装过MySQL,想再重新安装MySQL的时候可能就会因为前一版本卸载 ...
- POJ 3087 Shuffle'm Up bfs
题目链接:Shuffle'm Up 除了英文题有点恶心.发现模拟 + bfs 就可以过的时候,就是水了. 一个bug 就是filp函数得到string s12失败了.恩.据大腿告知,string 并不 ...
- Jenkins无法读取覆盖率报告的解决方法
报错信息如下: log 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 coverage-report: [mkdir] Cre ...
- 64位的ubuntu14.04 LTS安装 Linux交叉编译工具链及32位“ia32-libs”依赖库
ubuntu又迎来了其新一代的长期支持版本 14.04 LTS,其带来了许多令人期待的新特新,遂决定进行升级. 装好了64位版本及安装 Linux交叉编译工具链 运行GCC,${CROSS_COMPI ...
- Swift 获取plist文件展示在TableView上
// 1.定义二维数组 var data:[[String]]! override func viewDidLoad() { super.viewDidLoad() // 2.实例化tableView ...
- 使 docker 容器可以正常 mount privileged
[docker]privileged参数 privileged参数 $ docker help run ... --privileged=false Give extended privilege ...
- php浮点数比较
本文实例讲述了PHP中两个float(浮点数)比较方法.分享给大家供大家参考.具体如下: 最近在开发一个合同管理系统的时候,涉及到两个浮点数比较,算是把我郁闷惨了.在N久以前,就不晓得从哪里听来的一个 ...