当前位置:网站首页>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;
}
}
}
边栏推荐
- 07_ue4进阶_发射火球扣mp值和攻击扣血机制
- Qt智能指针易错点
- Interview focus - TCP protocol of transport layer
- Leetcode200 - find detailed explanation of the number of islands
- 寻找链表的中间节点
- Bubble sort idea and Implementation
- 行为型模式之责任链模式
- Binary tree 101. Symmetric binary tree
- Several ways of writing strings in reverse order
- Shardingsphere data slicing
猜你喜欢

The items of listview will be displayed completely after expansion

TOPSIS and entropy weight method

二叉树——101. 对称二叉树

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异

二叉树——104. 二叉树的最大深度

Iterator pattern of behavioral pattern

Find the cause of program dead cycle through debugging

复盘:推荐系统—— 负采样策略

什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。

Basic syntax of MySQL DDL, DML and DQL
随机推荐
Kubernetes网络插件详解 - Calico篇 - 概述
抽丝剥茧C语言(高阶)程序环境和预处理
浅识 OWASP
C - readonly and const keywords
老旧笔记本电脑变服务器(笔记本电脑+内网穿透)
STM32 lighting procedure
Lua脚本编写Wireshark插件解析第三方私有协议
Sort fake contacts
[learning notes] solid works operation record
A long detailed explanation of C language operators
Part 66: monocular 3D reconstruction point cloud
Binary tree 101. Symmetric binary tree
A brief introduction to OWASP
[learning notes] unreal 4 engine introduction (III)
Programming password guessing game
What is multithreading
C language actual combat guessing game
[one library] mapbox GL! A map engine out of the box
Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms
Unexpected dubug tricks