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 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- ios本地文件内容读取,.json .plist 文件读写
ios本地文件内容读取,.json .plist 文件读写 本地文件.json .plist文件是较为常用的存储本地数据的文件,对这些文件的操作也是一种常用的基础. 本文同时提供初始化变量的比较标准的 ...
- JavaSE复习日记 : 抽象类
/* * 抽象类 * 抽象: * 面向对象的三大核心思想; * 封装: 封装,ppp是封装的一部分 * 继承; * 多态: 父类的引用指向子类的对象 * 引用: 是指一个引用型变量 * 有哪些变量? ...
- I - Doing Homework again
I - Doing Homework again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- Hibernate总结(二)---hibernate中的各种映射
知识的准备: (参考:汤阳光Hibernate3.6(基础)) 一.浅谈javaBean之间的关系 存在的关系: 1.关联(一对一.一对多.多对一.多对多) 2.聚合/组合(最后考虑) 3.继承 所以 ...
- eclipse run on server 浏览器启动设置
- Yii2.0中文开发向导——自定义日志文件写日志
头部引入log类use yii\log\FileTarget; $time = microtime(true);$log = new FileTarget();$log->logFile = Y ...
- 基于KVM建立虚拟机的步骤及总结说明
1.前言 目前正在涉足云计算IaaS工作,虚拟化是IaaS的重要部分,因此这段时间对各个虚拟机化技术和工具进行研究,研究的目的不仅仅是为了会使用这个工具,而是通过研究了解技术的实现机制和原理,即知其然 ...
- Android SharedPreference最佳实践
Android提供多种方式保存应用数据,其中一种方式是SharedPreferences,使用键值对保存私有基本的数据.所有的逻辑仅基于以下三个类: SharedPreferences SharedP ...
- eclipse 插件安装
203.208.46.146 www.google.com203.208.46.146 dl.google.com203.208.46.146 dl-ssl.google.com
- linux下解压iso文件
.iso文件的格式是iso9660,iso9660是cd上的一种文件系统, 也就是说是 是数据在cd上的组织形式: 它的一些限制是: 1.最多8级子目录(可以用RockRidge Extension增 ...