当前位置:网站首页>字节面试题——判断一棵树是否为平衡二叉树
字节面试题——判断一棵树是否为平衡二叉树
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)
边栏推荐
- Etcd database source code analysis - cluster membership changes log
- 漫谈软件缺陷管理的实践
- 2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
- DOM operation -- operation node
- [paper notes] anti packet loss joint coding for network speech steganography
- Redis official visualization tool, with high appearance value and powerful functions!
- Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
- Ros2 knowledge: DDS basic knowledge
- 520 for what? DIY is a high-value RGB clock that girls want to watch
- Autumn move - Preparation Plan
猜你喜欢

知识沉淀一:架构师是做什么?解决了什么问题

Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
![[paper notes] anti packet loss joint coding for network speech steganography](/img/ca/95476b6d4b5765f5fde82cbeda577e.png)
[paper notes] anti packet loss joint coding for network speech steganography

对接微信支付(二)统一下单API

Full binary tree / true binary tree / complete binary tree~

Unity2D 动画器无法 创建过渡

You'd better not take this kind of project!

H. Take the Elevator 贪心

Lemon class automatic learning after all

Six sixths -- it's a little late and a little shallow
随机推荐
Lemon class automatic learning after all
Embedded general learning route arrangement
Motor control column summary
flex布局
Project topic selection reference
Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
ES Cluster in Red status: what about write & delete operations?
vagrant下载速度慢的解决方法
平衡二叉树(AVL) ~
【2023杰理科技提前批笔试题】~ 题目及参考答案
Hack the box - Introduction to networking module detailed Chinese tutorial
三本毕业,三年嵌入式软件的心路历程
漫谈软件缺陷管理的实践
Full binary tree / true binary tree / complete binary tree~
Redis持久化-RDB
Unity2D 动画器无法 创建过渡
Qt编写物联网管理平台47-通用数据库设置
Redis persistence AOF
Chapter 2 - getting started
How to view the container name in pod