Leetcode:110. 平衡二叉树
Leetcode:110. 平衡二叉树
Leetcode:110. 平衡二叉树
点链接就能看到原题啦~
关于AVL的判断函数写法,请跳转:平衡二叉树的判断
废话不说直接上代码吧~主要的解析的都在上面的链接里了
自顶向下写法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getHeight(TreeNode* root){
if(root==NULL) return 0;
return max(getHeight(root->right),getHeight(root->left))+1;
}
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
if(isBalanced(root->left)&&isBalanced(root->right))
if(abs(getHeight(root->left)-getHeight(root->right))<2)
return true;
return false;
}
};
自底向上写法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int isBalancedHelper(TreeNode* root,int& height){
if(root==NULL){
height=0;
return true;
}
int left,right;
if(isBalancedHelper(root->right,right)&&isBalancedHelper(root->left,left)&&abs(left-right)<2){
height=max(left,right)+1;
return true;
}
return false;
}
bool isBalanced(TreeNode* root) {
int height=0;
return isBalancedHelper(root,height);
}
};
Leetcode:110. 平衡二叉树的更多相关文章
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- Java实现 LeetCode 110 平衡二叉树
110. 平衡二叉树 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9 ...
- LeetCode 110.平衡二叉树(C++)
给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...
- [LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)
问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode:平衡二叉树【110】
LeetCode:平衡二叉树[110] 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 递归 - Leetcode 110 判断二叉树是否为平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
随机推荐
- crontab里的特殊符号%导致命令不能执行
有群里的小伙伴说crontab里的任务不执行,具体是这样的 * * * * /bin/date "+%Y-%m-%d %H:%M:%S" >>/data/tmp/tes ...
- Mac下使用Matplotlib无法显示中文的解决办法
参考:matplotlib图例中文乱码? - 知乎用户的回答 - 知乎 https://www.zhihu.com/question/25404709/answer/309784195 1.下载字体安 ...
- chrome清除缓存快捷键
CTRL+SHIFT+DEL:直接进入“清除浏览数据”页面,包括清除浏览历史记录.清空缓存.删除Cookie等. 进入开发者模式settings
- Html中div块居中显示
表面上这个问题很难,因为涉及到浏览器窗体大小,导致部分界面效果不一致.图中的方法适用于div块大小不变的界面. 如上所示,将其分为两块,margin-left和margin-top的值均分别为widt ...
- 深入学习MySQL 03 Schema与数据类型优化
Schema是什么鬼 schema就是数据库对象的集合,这个集合包含了各种对象如:表.视图.存储过程.索引等.为了区分不同的集合,就需要给不同的集合起不同的名字,默认情况下一个用户对应一个集合,用户的 ...
- 使用Razor表达式 使用条件语句 来自 精通ASP-NET-MVC-5-弗瑞曼
- Lobooi个人作业:阅读与准备作业
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业要求在哪里 https://edu.cn ...
- 用WORD批量制作工作证件
用WORD批量制作工作证件 一.采集电子照片 电子照片的采集要求以的名字作为照片的文件名,保存为“.jpg”格式,尺寸和大小需保持一致. 二.制作信息表 制作Exice数据信息表,包含姓名.年龄.部门 ...
- openresty http
openresty http openresty默认没有提供http客户端,需要第三方提供插件. 下载方式: wget https://raw.githubusercontent.com/pintsi ...
- Kubernetes-Ingress资源详解
什么是Ingress #阿里云称之为ingress路由!在 Kubernetes 集群中,主要用于接入外部请求到k8s内部,Ingress是授权入站连接到达集群服务的规则集合,为您提供七层负载均衡能力 ...