当前位置:网站首页>Byte interview question - judge whether a tree is a balanced binary tree
Byte interview question - judge whether a tree is a balanced binary tree
2022-07-26 05:54:00 【Study and pursue high efficiency】
Byte interview questions
4. Judge a tree Is it Balanced binary trees ( Time complexity :O(N^2))
***** Never mind recursive logic , Just write the code according to the top root node
// Get the height of the binary tree Time complexity :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);
}
// Time complexity :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);
}
Optimize , When there is an imbalance , No more recursion , But in turn return -1
Thinking angle when optimizing , Is to think from the bottom up
// This question It's the original question that byte passed 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;
}
***** Solve the mind
The original method is : Root node Compare the height of the left and right subtrees ,
then Then judge the height difference of the subtrees of the left and right subtrees , This will recurse twice The time complexity is (O N2)
Modification method : Is to traverse only once , When calculating the height of the tree , Determine whether it is a balanced binary tree , If not , Then stop calculating That is O(N)
边栏推荐
- Redis persistence RDB
- Chapter 1 - Construction of development environment
- 程序员如何改善精神内耗?
- Mongodb tutorial Chapter 08 comparison operators
- Is the transaction in mysql45 isolated or not?
- Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!
- vagrant下载速度慢的解决方法
- 如何查看Pod里容器名称
- MBA-day28 数的概念-练习题
- The idea YML file code does not prompt the solution
猜你喜欢

1.12 Web开发基础

Unity2D 动画器无法 创建过渡

Mysql45 talking about logging system: how does an SQL UPDATE statement execute?

DOM operation -- operation node

Mysql45 speak in simple terms index

ES Cluster in Red status: what about write & delete operations?

Select sort / insert sort / bubble sort

Mysql45 talking about infrastructure: how is an SQL query executed?

Jdbc流式查询与游标查询

Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
随机推荐
leetcode-Array
Six sixths -- it's a little late and a little shallow
Servlet filter details
平衡二叉树(AVL) ~
数据库sql语言实战
Unity Profiler
Use of feign (Part 2)
Should we test the Dao layer?
Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
5-year-old Test Engineer - how to choose the next step?
Is the transaction in mysql45 isolated or not?
Use latex to typeset multiple-choice test paper
MBA-day29 算术-绝对值初步认识
二叉排序树(BST) ~
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
Unity Profiler
[cloud native] introduction and use of feign of microservices
How can programmers improve mental internal friction?