/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
int h=isBal(root);
if(h!=-1){
return true;
}
else{
return false;
}
}
public int isBal(TreeNode root){
if(root==null){
return 0;
}
else{
int left=isBal(root.left);
int right=isBal(root.right);
if(left!=-1&&right!=-1&&Math.abs(left-right)<=1){
return Math.max(left,right)+1;
}
else{
return -1;
}
}
}
}
平衡二叉树判断
最新推荐文章于 2024-07-22 22:14:44 发布