Balanced Binary Tree——经典题
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.
递归,求左右是否平衡,再判断高度差
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: bool checkBalance(TreeNode *node, int &dep)
{
if (node == NULL)
{
dep = ;
return true;
} int leftDep, rightDep;
bool leftBalance = checkBalance(node->left, leftDep);
bool rightBalance = checkBalance(node->right, rightDep); dep = max(leftDep, rightDep)+; return leftBalance && rightBalance && (abs(rightDep - leftDep) <= );
}
bool isBalanced(TreeNode *root) {
int dep;
return checkBalance(root, dep); }
};
Balanced Binary Tree——经典题的更多相关文章
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree)/AVL树:
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
随机推荐
- SRM13 T3 花六游鸟小(结论题)
哇这题是真的喵,HR智商太高辣 这题的难点就是看了题解之后怎么证明题解里的结论... 结论①:深度大于logm的点肯定能达到最大值 证明:显然一个西瓜的属性里0数量一半1数量一半我们取到的1数量最少, ...
- pandas模块(数据分析)------dataframe
DataFrame DataFrame是一个表格型的数据结构,含有一组有序的列,是一个二维结构. DataFrame可以被看做是由Series组成的字典,并且共用一个索引. 一.生成方式 import ...
- PHP汉字转拼音
<?php/** *+------------------------------------------------------ * PHP 汉字转拼音 *+----------------- ...
- JavaScript对iframe的DOM操作
在IE6.IE7中,我们可以使用 document.frames[ID].document 来访问iframe子窗口中的document对象,可是这是不符合W3C标准的写法,也是IE下独有的方法,在F ...
- springboot集成junit测试与javamail测试遇到的问题
1.springboot如何集成junit测试? 导入junit的jar包 使用下面注解: @RunWith()关于这个的解释看下这两篇文章: http://www.imooc.com/qadetai ...
- ubuntu server安装kvm
参考资料: 1. https://help.ubuntu.com/community/KVM 2.http://wiki.ubuntu.org.cn/Kvm%E6%95%99%E7%A8%8B 3.h ...
- 在Linux防火墙上过滤外来的ICMP timestamp
ICMP timestamp请求响应漏洞 解决方案: * 在您的防火墙上过滤外来的ICMP timestamp(类型13)报文以及外出的ICMP timestamp回复报文. 具体解决方式就 ...
- android Handler post sendMessage
Handler 为Android操作系统中的线程通信工具,包为android.os.Handler. 与Handler绑定的有两个队列,一个为消息队列,另一个为线程队列.Handler可以通过这两个队 ...
- Enterprise Architect 13 : 将绘制的图形导出成图片 或者 拷贝到剪贴板中
使用Enterprise Architect 13 工具画类图.包图.需求图.状态图.时序图什么的导出成图片格式 或者拷贝到剪贴版,步骤如下: Publish -> Save Image -&g ...
- MyBatis注解Annotation介绍及Demo
MyBatis注解Annotation介绍及Demo 2014-04-21 17:09:55 标签:Mybatis Annotation 注解 ResultMap SqlBuilder 原创作品,允 ...