#yyds干货盘点# 解决剑指offer: 判断是不是平衡二叉树
source link: https://blog.51cto.com/u_15488507/5410326
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
#yyds干货盘点# 解决剑指offer: 判断是不是平衡二叉树
原创1.简述:
输入一棵节点数为 n 二叉树,判断该二叉树是否是平衡二叉树。在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树平衡二叉树(Balanced Binary Tree),具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
样例解释:
样例二叉树如图,为一颗平衡二叉树
注:我们约定空树是平衡二叉树。
数据范围:,树上节点的val值满足 要求:空间复杂度,时间复杂度
输入描述:
输入一棵二叉树的根节点
返回值描述:
输出一个布尔类型的值
2.代码实现:
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null){
return true;
}
int left = deep(root.left);
int right = deep(root.right);
int v = Math.abs(left - right);
if(v > 1){
return false;
}
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
private int deep(TreeNode root) {
if(root == null){
return 0;
}
return Math.max(deep(root.left), deep(root.right)) + 1;
}
}
Recommend
-
3
#yyds干货盘点# 解决剑指offer:机器人的运动范围 原创 97的风 2022-04-22 11:28:21...
-
3
#yyds干货盘点# 解决剑指offer:数组中出现次数超过一半的数字 原创 97的风
-
3
#yyds干货盘点# 解决剑指offer:字符串的排列 原创 97的风 2022-05-08 23...
-
3
#yyds干货盘点# 解决剑指offer:最小的K个数 原创 97的风 2022-05-10 10:...
-
3
#yyds干货盘点# 解决剑指offer:数字在升序数组中出现的次数 原创 97的风
-
6
#yyds干货盘点# 解决剑指offer:二叉搜索树的第k个节点 原创 97的风 2022...
-
7
#yyds干货盘点# 解决剑指offer:左旋转字符串 原创 97的风 2022-05-30 10...
-
1
#yyds干货盘点# 解决剑指offer:和为S的两个数字 原创 97的风 2022-05-29...
-
1
#yyds干货盘点# 解决剑指offer:跳台阶 原创 97的风 2022-06-08 10:54:16...
-
4
#yyds干货盘点# 解决剑指offer:跳台阶扩展问题 原创 97的风 2022-06-10...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK