剑指offer No.39 平衡二叉树

2022-11-26 11:05:07 浏览数 (1)

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

代码语言:javascript复制
package offer.IsBalanced_Solution;

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        boolean rs=false;
        if(root==null){
            rs=true;
        }else{
            int leftDepth=depth(root.left,0);
            int rightDepth=depth(root.right,0);
            if(Math.abs(leftDepth-rightDepth)<=1){
                rs=true;
            }
        }
        return rs;
    }
    public int depth(TreeNode root,int depth){
        if(root==null){
            return depth;
        }
        TreeNode leftChild=root.left;
        TreeNode rightChild=root.right;
        int leftDepth;
        if(leftChild!=null){
            leftDepth=depth(root.left,depth 1);
        }else{
            leftDepth=depth;
        }

        int rightDepth;
        if(rightChild!=null){
            rightDepth=depth(root.right,depth 1);
        }else{
            rightDepth=depth;
        }
        return Math.max(leftDepth,rightDepth);
    }
}

0 人点赞