平衡二叉树判断

/** * 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;
            }
        }
    }
}
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/xiejinfeng2013/article/details/81747890
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞