LeetCode 110. Balanced Binary Tree(判断平衡二叉树)
2024-09-02 10:07:59
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example 1:
Given the following tree [3,9,20,null,null,15,7]
:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]
:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
/**
* 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 depth(TreeNode *root)
{
if (root == NULL) return ;
return max(depth(root->left), depth(root->right)) + ;
} bool isBalanced(TreeNode *root)
{
if (root == NULL) return true; int left = depth(root->left);
int right = depth(root->right); return abs(left - right) <= && isBalanced(root->left) && isBalanced(root->right); }
};
最新文章
- Theano3.3-练习之逻辑回归
- Yocto开发笔记之《工具使用:TFTP &; NFS &; SSH》(QQ交流群:519230208)
- 百度云+ KeePass 网络同步你的密码
- 【随笔】使用mOnOwall封禁某一个ip
- python数据结构-列表-基本操作
- javascript/jquery判断是否为undefined或是null!
- jquery动态移除/增加onclick属性详解
- 关于使用用友华表Cell控件按需打印行的方法
- php基础知识【函数】(7)url和ob函数
- HDU 3338 Kakuro Extension
- Clustering text documents using k-means
- cassandra 在window上的demo
- Springboot集成Thymeleaf
- 【小o地图Excel插件版】不止能做图表,还能抓58、大众点评网页数据...
- 7 种 join
- C#设计模式(7)——适配器模式(Adapter Pattern)(转)
- Apache Spark 2.2.0 新特性详细介绍
- SPRING的事务配置详解
- <;fmt:formatNumber>;标签
- [Algorithm] Print 2-D array in spiral order