public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
//空树也为平衡二叉树
if(root == null) return true;
int l = deep(root.left);
int r = deep(root.right);
if((l-r) > 1 || (r-l) > 1) return false;
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
private int deep(TreeNode root){
//空节点深度为0
if(root == null) return 0;
//递归计算
int left = deep(root.left);
int right = deep(root.right);
//找出最大的深度加上自己
return (left > right) ? left+1 : right+1;
}
}
力扣算法25——JZ79 判断是不是平衡二叉树
最新推荐文章于 2024-11-11 20:59:17 发布