当前位置:网站首页>[Jianzhi offer] 55 - ii balanced binary tree
[Jianzhi offer] 55 - ii balanced binary tree
2022-07-01 13:39:00 【LuZhouShiLi】
The finger of the sword Offer 55 - II. Balanced binary trees
subject
Enter the root node of a binary tree , Judge whether the tree is a balanced binary tree . If the depth difference between the left and right subtrees of any node in a binary tree does not exceed 1, So it's a balanced binary tree .
Ideas
First, define a function to calculate the height of nodes , Then traverse according to the preorder of the binary tree , 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 1, Recursively traverse the left and right child nodes respectively , And judge whether the left and right subtrees are balanced .
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
class Solution {
public:
int height(TreeNode* root)
{
if(root == NULL)
{
return 0;
}
else
{
// Calculate the height of the binary tree
return max(height(root->left),height(root->right)) + 1;
}
}
bool isBalanced(TreeNode* root)
{
if(root == NULL)
{
return true;
}
else
{
return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
}
};
边栏推荐
- Grafana reports an error: error= "failed to send notification to email addresses: [email protected] : 535 Error:
- 进入前六!博云在中国云管理软件市场销量排行持续上升
- 研发效能度量框架解读
- Wave animation color five pointed star loader loading JS special effects
- AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
- Analysis report on production and marketing demand and investment forecast of global and Chinese diamond powder industry Ⓤ 2022 ~ 2027
- Who should I know when opening a stock account? Is it actually safe to open an account online?
- Global and Chinese n-butanol acetic acid market development trend and prospect forecast report Ⓧ 2022 ~ 2028
- Camp division of common PLC programming software
- 介绍一种对 SAP GUI 里的收藏夹事务码管理工具增强的实现方案
猜你喜欢
基于mysql乐观锁实现秒杀的示例代码

Simple two ball loading

Jenkins+webhooks- multi branch parametric construction-

Beidou communication module Beidou GPS module Beidou communication terminal DTU

MySQL 66 questions, 20000 words + 50 pictures in detail! Necessary for review

5G工业网关的科技治超应用 超限超重超速非现场联合执法

What is the future development direction of people with ordinary education, appearance and family background? The career planning after 00 has been made clear

Flow management technology

Colorful five pointed star SVG dynamic web page background JS special effect

1553B环境搭建
随机推荐
进入前六!博云在中国云管理软件市场销量排行持续上升
SAP 智能机器人流程自动化(iRPA)解决方案分享
When you really learn databinding, you will find "this thing is really fragrant"!
Yarn restart applications record recovery
Apache-atlas-2.2.0 independent compilation and deployment
MySQL六十六问,两万字+五十图详解!复习必备
龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
A Fletter version of Notepad
LeetCode重建二叉树详解[通俗易懂]
8款最佳实践,保护你的 IaC 安全!
Google Earth Engine(GEE)——全球人类居住区网格数据 1975-1990-2000-2014 (P2016)
2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
学会使用LiveData和ViewModel,我相信会让你在写业务时变得轻松
【241. 为运算表达式设计优先级】
ArrayList扩容机制以及线程安全性
3.4 《数据库系统概论》之数据查询—SELECT(单表查询、连接查询、嵌套查询、集合查询、多表查询)
spark源码(五)DAGScheduler TaskScheduler如何配合提交任务,application、job、stage、taskset、task对应关系是什么?
当你真的学会DataBinding后,你会发现“这玩意真香”!
Introduction to topological sorting
ArrayList capacity expansion mechanism and thread safety