当前位置:网站首页>Force buckle 110. Balanced binary tree
Force buckle 110. Balanced binary tree
2022-07-27 06:07:00 【The last tripod】
Portal 110. Balanced binary trees
Their thinking
Comparing the depth difference between the left and right subtrees of each node, we must recursively find the depth of the left and right subtrees , After finding the depth of both left and right nodes, directly compare , If false Just put flag Marked as false, If there is no mark at the end of the program , It shows that the binary tree is a balanced binary tree
Code
class Solution {
boolean flag = true;
public int getSize(TreeNode node){
if(node == null)return 0;
int lSize = getSize(node.left);
int rSize = getSize(node.right);
if(Math.abs(lSize - rSize) > 1)flag = false;
return 1 + Math.max(lSize,rSize);
}
public boolean isBalanced(TreeNode root) {
if(root == null)return true;
getSize(root);
return flag;
}
}
- Time complexity O(n);
- Spatial complexity O(n);// Recursive space
边栏推荐
猜你喜欢

STM32 infrared remote control

PZK学C语言之数据类型,进制转换,输入输出,运算符,分支语句ifelse

力扣每日一题 剑指 Offer II 091. 粉刷房子

Unity Shader 概述

What tools are needed to make video post effects?

Lightroom classic 2022 v11.4 Chinese version "latest resources"

Weidongshan digital photo frame project learning (IV) simple TXT document display (e-paper book)

发布 分辨率0.22m的建筑物分割数据库

What has been updated in the Chinese version of XMIND mind map 2022 v12.0.3?

非重叠矩形中的随机点(力扣每日一题)
随机推荐
如何管理大量的定时任务
[first song] machine learning of rebirth - linear regression
[song] rebirth of me in py introductory training (10): numpy
[first song] rebirth of me in py introductory training (3): if conditional sentence
力扣题解 单调栈
【头歌】重生之我在py入门实训中(5):列表
【头歌】重生之深度学习篇-Keras(初级)
Auto Encoder(AE),Denoising Auto Encoder(DAE), Variational Auto Encoder(VAE) 区别
Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis
2022.6.10 stm32mp157 serial port clock learning
C语言-动态内存管理
Cmder的基础文件操作
Baiwen driver Daquan learning (I) LCD driver
力扣题解 二叉树(8)
力扣题解 动态规划(7)
LaTeX中多个公式公用一个序号时
C语言-程序的编译
发布 分辨率0.22m的建筑物分割数据库
制作视频特效必备工具:NUKE 13
物联网操作系统多任务基础