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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- openv+contrib配置总结
本文转载于:https://www.cnblogs.com/wjy-lulu/p/6805557.html 开门见山的说:别用opencv3.0,这个版本添加扩展库不怎么好,能不能成功我不敢说,我是试 ...
- echarta3 北京,上海地图
1.首先你得到echarts官网下载js,建议下载完整代码,这样你就很容易根据我的路径找到beijing.js 2.把echarts.js和beijingi.js根据你的路径引对,然后就可以copy我 ...
- springboot问题集(一)------junit内Assert.assertEquals()的含义
1. assertEquals([String message],Object target,Object result) target与result不相等,中断测试方法,输出message asse ...
- oracle用户 密码永不过期
ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;
- poj3680
题解: 相邻的建边 每一段建边 然后见一个原点,汇点 代码: #include<cstdio> #include<cmath> #include<cstring> ...
- bzoj1037
题解: 定义f[i][j][a][b]表示已经排了i个人 还能拍j个男的(那么就还有m-i+j个是女的) 还能连续拍a个男的,b个女的 我是递推的 考虑后面一个拍男的还是女的 注意要判断边界 代码: ...
- istringstream、ostringstream、stringstream类介绍
本文转载自: http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html 一.C++的输入输出分为三种: (1)基于控制台的I/O ( ...
- 433.92 TX RX module design and test recording。。
This paper records the process of 433.92 TX RX module design and test,fyi. 1 RX module The circuit ...
- ubuntu16.04安装Nvidia显卡驱动、CUDA8.0和cudNN V6
Nvidia显卡驱动安装 在ubuntu搜索框输入 软件更新,打开 "软件和更新" 对话框,在 附加驱动里选择系统检测到的Nvidia驱动,应用更改,重启系统: 安装完成之后查看G ...
- mybatis异常:Could not find result map Java.util.Map 问题分析及解决 定位不到具体的位置的错误
mybatis异常:Could not find result map Java.util.Map 问题分析及解决 报这个错误呢,很难受的就是你定位不到具体的地方,找不到位置修改,你只知道有错误,但是 ...