当前位置:网站首页>判断是否为平衡二叉树
判断是否为平衡二叉树
2022-06-11 18:01:00 【AlbertOS】
前言
这道题中的平衡二叉树的定义是:二叉树的每个节点的左右子树的高度差的绝对值不超过 11,则二叉树是平衡二叉树。根据定义,一棵二叉树是平衡二叉树,当且仅当其所有子树也都是平衡二叉树,因此可以使用递归的方式判断二叉树是不是平衡二叉树,递归的顺序可以是自顶向下或者自底向上。
自底向上的递归
方法一由于是自顶向下递归,因此对于同一个节点,函数 h e i g h t height height会被重复调用,导致时间复杂度较高。如果使用自底向上的做法,则对于每个节点,函数 h e i g h t height height只会被调用一次。
**自底向上递归是对于当前遍历到的节点,先递归地判断其左右子树是否平衡,再判断以当前节点为根的子树是否平衡。**如果一棵子树是平衡的,则返回其高度(高度一定是非负整数),否则返回 −1。如果存在一棵子树不平衡,则整个二叉树一定不平衡。
class Solution {
public boolean isBalanced(TreeNode root) {
return height(root) >= 0;
}
public int height(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
return -1;
} else {
return Math.max(leftHeight, rightHeight) + 1;
}
}
}
复杂度分析
时间复杂度:O(n),其中 n 是二叉树中的节点个数。使用自底向上的递归,每个节点的计算高度和判断是否平衡都只需要处理一次,最坏情况下需要遍历二叉树中的所有节点,因此时间复杂度是O(n)。
空间复杂度:O(n),其中 n 是二叉树中的节点个数。空间复杂度主要取决于递归调用的层数,递归调用的层数不会超过 n。
优化
这里对于右边的部分,我们还可以加上剪枝的操作,而使这个算法更快
class Solution {
public boolean isBalanced(TreeNode root) {
return height(root) >= 0;
}
public int height(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = leftHeight==-1?-1: height(root.right);//与左边的高度比较,如果大于-1我们就可以剪掉剩余的部分
if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
return -1;
} else {
return Math.max(leftHeight, rightHeight) + 1;
}
}
}
边栏推荐
- Winter vacation daily question (improvement group) [end of week 4]
- 单选按钮 文字背景同时改变
- Install MariaDB 10.5.7 (tar package installation)
- [piecemeal knowledge] [network composition] the mobile phone can be connected to the campus network, but the computer can't
- [collect first and use it sooner or later] 100 Flink high-frequency interview questions series (III)
- The maximum (minimum, latest and top n) records are taken for MySQL grouping
- The top ten trends of 2022 industrial Internet security was officially released
- vulhub
- [pat grade B question bank] complete summary
- Spring 2021 daily question [week7 not finished]
猜你喜欢

mysql8安装,navicat安装,sqli-labs搭建
![[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)
![Spring 2021 daily question [week5 not finished]](/img/bd/35a8e0ded3b1a0727415c4cd95e781.jpg)
Spring 2021 daily question [week5 not finished]
![Codeworks round 479 (Div. 3) [done]](/img/a0/f3c6989d8f755c03076b237514ee64.jpg)
Codeworks round 479 (Div. 3) [done]

SQL error injection 1
![[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration](/img/ae/9a0c300f2dcb03b05d737f14b0955f.jpg)
[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration

SISO decoder for a general (n,n-1) SPC code(補充章節3)

ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
![[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)
![[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)](/img/92/03de54c9f08eae5bc920b04d2b8493.png)
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
随机推荐
Hwang
[C语言]用结构体把平均分和低于等于平均分的学生数据输出
Initial egg framework
Spring 2021 daily question [week7 not finished]
[C语言]用结构体按分数高低降序输出学生的姓名和分数
SISO Decoder for SPC (补充章节1)
【C】 Compilation preprocessing and environment
Mysql8 installation, Navicat installation, sqli labs setup
HashSet集合存储学生对象并遍历
Online excel file parsing and conversion to JSON format
After class, I looked at the document and went back to the lab. I picked up the forgotten SQL operators again
網絡安全威脅情報體系
Radiogroup dynamically add RadioButton
SISO Decoder for Repetition(补充章节4)
Getting started with CTF
Simple understanding of events
"College entrance examination" volume sent to the big model: 442 people put forward 204 tasks to the big model, led by Google
密码学概述
LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
Codeworks round 481 (Div. 3) [done]