Balanced Binary Tree --Leetcode C++
递归
- 左子树是否为平衡二叉树
- 右子树是否为平衡二叉树
- 左右子树高度差是否大于1,大于1,返回false
- 否则判断左右子树
最简单的理解方法就是如下的思路:
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL){
return true;
}
int ldepth=depth(root->left);
int rdepth=depth(root->right);
if(abs(ldepth-rdepth)>)return false;
else{
return isBalanced(root->left)&&isBalanced(root->right);
}
}
int depth(TreeNode *root){
if(root==NULL){
return ;
}
int lleft=depth(root->left);
int trigth=depth(root->right);
return lleft>trigth ? lleft+:trigth+;
}
};
在上面的方法中,每一个节点都会计算节点左子树和右子树的高度,存在大量的重复计算
所以,采用自底向上的方式
class Solution2 {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL){
return true;
}
int height=getheight(root);
if(height==-){
return false;
}
return true;
}
int getheight(TreeNode *root){
if(root==NULL){
return ;
}
int lleft=getheight(root->left);
int trigth=getheight(root->right);
if(lleft==-||trigth==-){
return -;
}
if(abs(lleft-trigth)>){
return -;
}
return lleft>trigth? lleft+:trigth+;
}
};
Balanced Binary Tree --Leetcode C++的更多相关文章
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Balanced Binary Tree [LeetCode]
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree——LeetCode
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree leetcode java
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- http 请求安全
在info.plist中加入 <key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbit ...
- 最小生成树Jungle Roads
这道题一定要注意录入方式,我用的解法是prime算法 因为单个字符的录入会涉及到缓冲区遗留的空格问题,我原本是采用c语言的输入方法录入数据的,结果对了,但是提交却一直wrong,后来改成了c++的ci ...
- 收集的jquery插件
1.精美jQuery分页插件 带滑动条分页使用HTML5实现刮刮卡效果 今天开始我们来收集一些jQuery分页插件,今天第一款jQuery分页插件适用于长翻页列表的分页应用,因为这款分页插件带有滑动条 ...
- codeforces 630P. Area of a Star
题目链接 圆上n个点等距离分布, 求构成的星星的面积. 我们可以求三角形OAB的面积, ∠CAE = 1/2 ∠ COE = PI/n, 那么∠CAO = PI/2n, ∠AOB非常好求, 就是PI/ ...
- 数字运算、ASCII
num20 = dollar/20;num10 = (dollar - 20*num20)/10;num5 =(dollar-20*num20-10*num10)/5;//可以写为num5 = (do ...
- SQL 设计心得、逗号分隔列表
第一: 在开始编码前.主要关注数据库里要保存什么样的数据,以级最佳的数据组织方式和内在关联方式. 第二: 使用你所知的数据库特性尽可能高效的实现数据管理.如正确的索引.数据库类型.高效的select! ...
- MockObject
http://www.mockobjects.com/ http://jmock.org/download.html https://jakarta.apache.org/cactus/mock_vs ...
- VS2015如何另存解决方案文件-修改解决方案sln文件的路径
原文:VS2005如何另存解决方案文件-修改解决方案sln文件的路径 修改解决方案sln文件的路径 方法一:工具→选项→项目和解决方案,可设置项目的默认保存位置.方法二:"解决方案资源管理器 ...
- css hr 设置
http://www.sovavsiti.cz/css/hr.html http://adamculpepper.net/blog/css/hr-tag-css-styling-cross-brows ...
- python 入门快速学习整理
Python 入门学习 1 : 对象类型 1 1.1 列表 1 1.2 字典 2 1.3 元组 2 1.4 元组 2 1.4 文件 3 2 : 条件和循环语句 3 2.1 if else语句 3 ...