当前位置:网站首页>Binary tree - 110. Balanced binary tree
Binary tree - 110. Balanced binary tree
2022-07-26 00:04:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given a binary tree , Determine if it's a highly balanced binary tree .
In this question , A height balanced binary tree is defined as :
Every node of a binary tree The absolute value of the height difference between the left and right subtrees is not more than 1 .
2 Title Example


Example 3:
Input :root = []
Output :true
3 Topic tips
The number of nodes in the tree is in the range [0, 5000] Inside
-104 <= Node.val <= 104
4 Ideas
The definition of balanced binary tree in this problem is : The absolute value of the height difference between the left and right subtrees of each node of the binary tree does not exceed 11, Then the binary tree is a balanced binary tree . According to the definition , A binary tree is a balanced binary tree , If and only if all its subtrees are balanced binary trees , Therefore, we can use the recursive method to judge whether the binary tree is a balanced binary tree , The order of recursion can be top-down or bottom-up .

With the function of calculating the height of nodes , We can judge whether the binary tree is balanced . The specific method is similar to the preorder traversal of binary tree , That is, for the node currently traversed , First, calculate the height of the left and right subtrees , If the height difference between the left and right subtrees does not exceed 11, Then recursively traverse the left and right child nodes , And judge whether the left subtree and the right subtree are balanced . This is a top-down recursive process .
Complexity analysis
Time complexity :o(n²), among n Is the number of nodes in the binary tree . In the worst case , A binary tree is a full binary tree , You need to traverse all nodes in the binary tree , The time complexity is O(n).
For the node p, If its height is d, be height§ At most d Time ( That is, when traversing each of its ancestor nodes ). For the average case , The height of a tree h Satisfy O(h)= O(logn), because d<h, So the total time complexity is O(n logn). For the worst case , Binary trees form a chain structure , The height is O(n), At this time, the total time complexity is o(n²).
Spatial complexity :O(n), among n Is the number of nodes in the binary tree . The space complexity mainly depends on the number of layers of recursive calls , The number of layers of recursive calls will not exceed n.
5 My answer
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
} else {
return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
}
public int height(TreeNode root) {
if (root == null) {
return 0;
} else {
return Math.max(height(root.left), height(root.right)) + 1;
}
}
}
边栏推荐
- LeetCode 刷题系列 -- 931. 下降路径最小和
- SQLZOO——Nobel Quiz
- 结对编程实践心得
- [learning notes] solid works operation record
- Sequence traversal II of leetcode107 binary tree
- 【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
- BGR and RGB convert each other
- NVIDIA cuDNN学习
- Song of statistics lyrics
- Key and difficult points of C language pointer
猜你喜欢

“群魔乱舞”,牛市是不是结束了?2021-05-13

String functions and memory operation functions

回溯——77. 组合

二叉树——226. 翻转二叉树

Find the cause of program dead cycle through debugging

Stm32- analyze latency based on assembly

Redis extended data type (jump table /bitmaps/hyperloglog/geospatial)

The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function

初阶C语言 - 分支语句(if、switch)

Leetcode169-多数元素详解
随机推荐
QT smart pointer error prone point
行为型模式之迭代器模式
二叉树——257. 二叉树的所有路径
Getaverse,走向Web3的远方桥梁
寻找链表的中间节点
Leetcode169 detailed explanation of most elements
回溯——17. 电话号码的字母组合
【英雄哥七月集训】第 24天: 线性树
行为型模式之观察者模式
如何用yolov5 做个闯红灯监控的智能交通系统(1)
Find the cause of program dead cycle through debugging
复盘:推荐系统—— 负采样策略
浅识 OWASP
The process of finding free screen recording software - I didn't expect win10 to come with this function
Basic syntax of MySQL DDL, DML and DQL
牛市还没有结束,还有下半场 2021-05-18
Iterator pattern of behavioral pattern
SIGIR‘22 推荐系统论文之图网络篇
LeetCode_55_跳跃游戏
VSCode格式化Json文件