当前位置:网站首页>字节面试题——判断一棵树是否为平衡二叉树
字节面试题——判断一棵树是否为平衡二叉树
2022-07-26 05:54:00 【学习追求高效率】
字节面试题
4. 判断一棵树 是否为 平衡二叉树(时间复杂度:O(N^2))
*****不用管递归的逻辑,只需根据最上边根节点写出代码即可
// 获取二叉树的高度 时间复杂度:O(N)
int getHeight(TreeNode root) {
if(root == null) return 0;
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
return (leftHeight > rightHeight ?
leftHeight+1 : rightHeight+1);
}
//时间复杂度:O(N^2)
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
return Math.abs(leftHeight-rightHeight) <= 1
&& isBalanced(root.left)
&& isBalanced(root.right);
}
优化,当出现不平衡情况,便不再进行递归,而是依次return -1
优化时候的思考角度,便是从下往上思考
//这个题 是字节考过的原题 O(n)
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftTree = maxDepth(root.left);
int rightTree = maxDepth(root.right);
if(leftTree >= 0 && rightTree >= 0 && Math.abs(leftTree - rightTree) <= 1) {
return Math.max(leftTree,rightTree) + 1;
}else {
return -1;
}
}
public boolean isBalanced2(TreeNode root) {
if(root == null) return true;
return maxDepth(root) >= 0;
}
***** 解决思想
原方法是:求根节点的 左右子树的高度后比较,
然后 再去判断左右子树的子树的分别高度差,这样就会递归两次 时间复杂度为(O N2)
修改方法:是只遍历一次,在求树的高度的时候,判断是否为平衡二叉树,如果不是,便停止计算 便是 O(N)
边栏推荐
- 高手是怎样炼成的?
- 某公司给每个工位装监控:只为看员工写代码?
- 一招教你轻松看懂波特图
- Use latex to typeset multiple-choice test paper
- Realize channel routing based on policy mode
- 金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
- Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
- Ros2 knowledge: DDS basic knowledge
- Etcd database source code analysis - cluster membership changes log
- Redis persistence RDB
猜你喜欢

我又发现了超赞的软硬件项目,全部开源

leetcode-aboutString

Redis transaction

Mysql45 speak in simple terms index

Mba-day29 arithmetic - preliminary understanding of absolute value

某公司给每个工位装监控:只为看员工写代码?

语法泛化三种可行方案介绍
![[MySQL must know and know] time function number function string function condition judgment](/img/b2/aa15bf4cd78a3742704f6bd5ecb9c6.png)
[MySQL must know and know] time function number function string function condition judgment

520 for what? DIY is a high-value RGB clock that girls want to watch

Etcd database source code analysis - cluster membership changes log
随机推荐
Redis transaction
K. Link with Bracket Sequence I dp
vagrant下载速度慢的解决方法
Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
Rocbossphp free open source light community system
JDBC streaming query and cursor query
[cloud native] record of feign custom configuration of microservices
You'd better not take this kind of project!
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
leetcode-aboutString
Benji Banas launched the second season of earn while playing bonus activities, supporting the use of multiple Benji passes!
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
Hack the box - Introduction to networking module detailed Chinese tutorial
The idea YML file code does not prompt the solution
Is it really hopeless to choose electronic engineering and be discouraged?
Kingbasees SQL language reference manual of Jincang database (6. Expression)
语法泛化三种可行方案介绍
Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!
[STM32 series summary] blogger's way to quickly advance STM32 in actual combat (continuous update)
LNMP architecture