当前位置:网站首页>Leetcode 110. balanced binary tree
Leetcode 110. balanced binary tree
2022-07-23 09:38:00 【LuZhouShiLi】
Leetcode 110. Balanced binary trees
subject
Given a binary tree , Determine if it's a highly balanced binary tree .
Ideas
- Specify the parameters and return values of recursive functions : The parameter is the passed in node pointer , The return value is the height of the binary tree with this node as the root node
- Specify termination conditions : Return when encountering empty node 0, Indicates that the height of the tree with the current node as the root node is 0
- Explicit single-layer recursive logic : Judge the height difference between the left and right subtrees , If the difference is less than or equal to 1, Returns the height of the current binary tree
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
// Returns the height of the binary tree with this node as the root node
int getDepth(TreeNode *node)
{
if(node == NULL)
{
return 0;
}
int leftDepth = getDepth(node->left);
if(leftDepth == -1)
{
return -1;// It shows that the left subtree is no longer a balanced binary tree
}
int rightDepth = getDepth(node->right);
if(rightDepth == -1)
{
return -1;// It shows that the right subtree is no longer a balanced binary tree
}
return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth,rightDepth);
}
bool isBalanced(TreeNode* root) {
return getDepth(root) == -1 ? false:true;
}
};
边栏推荐
- 复盘:一副牌(54张),三人斗地主,大小王在同一家的概率是多少
- 程序环境和预处理
- 广发期货可以开户吗?安全吗
- 不同类型的字段、集合list/set/map、对象如何判空null
- 服务器内存性能调优
- A ConvNet for the 2020s 论文阅读
- 600 English words that programmers must master
- 使用HiFlow场景连接器查看每天处于地区的疫情
- 微信小程序设置背景图片不显示问题解决方法
- Event listening and deleting events - event object - default event - cancel bubbling event - event delegation - default trigger
猜你喜欢

肽核酸(PNA)偶联穿膜肽(CCPs)(KFF)3K形成CCPs-PNA|肽核酸的使用方法

判断两个类型是否相同

wallys/WiFi6 MiniPCIe Module 2T2R2×2.4GHz 2x5GHz MT7915 MT7975

力扣(LeetCode)203. 移除链表元素(2022.07.22)

PyG利用MessagePassing搭建GCN实现节点分类

线性反馈移位寄存器(LSFR)

PNA修饰多肽Bz-Val-Gly-Arg-PNA|Boc-Val-Leu-Gly-Arg-PNA

一、buildroot目录结构

Q-Vision+Kvaser CAN/CAN FD/LIN总线解决方案

Event listening and deleting events - event object - default event - cancel bubbling event - event delegation - default trigger
随机推荐
Keyword Driven
复盘:pearson皮尔森相关系数和spearman斯皮尔曼相关系数的区别
How to learn MySQL efficiently and systematically?
How to determine the end point of a software test
真人踩过的坑,告诉你避免自动化测试常犯的10个错误
Weekly recommended short video: why write such a book?
BeanSearcher接收数组参数、以及逻辑删除
Transformer summary
Leetcode 110. 平衡二叉树
35 year old programmer, early middle-aged crisis
Can GF futures open an account? Is it safe?
Program environment and pretreatment
【bug 简单处理】
Advantages of BGP machine room
来看看 VSCode 的多行编辑
Avantages de la salle des machines bgp
IBM:到2030年实现容错量子优势
canal 第6篇
Selenium.webdriver gets the result and converts it to JSON format
selenium.webdriver获取结果转为json格式