平衡二叉树

来源:互联网 发布:黑苹果mac server 硬件 编辑:程序博客网 时间:2024/05/21 06:42


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


public class Solution {    private boolean isBalanced = true;    public boolean IsBalanced_Solution(TreeNode root) {        getDepth(root);        return isBalanced;    }        private int getDepth(TreeNode root) {        if (root == null)            return 0;        int left = getDepth(root.left);        int right = getDepth(root.right);                if (Math.abs(left-right) > 1)            isBalanced = false;        return right > left ? right + 1 : left + 1;    }}


原创粉丝点击