当前位置:网站首页>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;
}
}
}
边栏推荐
- Bubble sort idea and Implementation
- 栈与队列——239. 滑动窗口最大值
- @Autowired注解的底层原理
- Exercise (1) create a set C1 to store the elements "one", "two", "three"“
- Stack and queue - 347. Top k high frequency elements
- 栈与队列——347. 前 K 个高频元素
- Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
- LeetCode 沙胡同系列 -- 63. 不同路径 II
- 行为型模式之观察者模式
- 【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
猜你喜欢

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

The mobile version of Duoyu security browser will add new functions to make users browse more personalized

07_ue4进阶_发射火球扣mp值和攻击扣血机制

Responsibility chain model of behavioral model

Leetcode200-查找岛屿数量详解

How to use yolov5 as an intelligent transportation system for red light running monitoring (1)

回溯——17. 电话号码的字母组合

二叉树——112. 路径总和

最近随感,关于牛市和DeFi 2021-05-17

Article 75: writing skills of academic papers
随机推荐
The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
二叉树——112. 路径总和
String functions and memory operation functions
Stm32- analyze latency based on assembly
Kubernetes网络插件详解 - Calico篇 - 概述
Cherish time and improve efficiency
Detailed explanation of kubernetes network plug-ins - calico chapter - Overview
Chapter 64: error lnk2019: unresolved external symbol cvround
Are you still using your browser's own bookmarks? This bookmark plugin is awesome
Ten threats to open API ecosystem
Stack and queue - 239. Sliding window maximum
SIGIR '22 recommendation system paper graph network
YoloV4-tiny网络结构
Responsibility chain model of behavioral model
Iterator pattern of behavioral pattern
C language actual combat guessing game
复盘:推荐系统—— 负采样策略
二叉树——104. 二叉树的最大深度
Nacos 下线服务时报错 errCode: 500
Binary tree 101. Symmetric binary tree