Easy-题目24:110. Balanced Binary Tree

题目原文:
Given a binary tree, determine if it is height-balanced.
题目大意:
判断一个树是不是二叉平衡树。
题目分析:
二叉平衡树可递归定义如下:
(1) 空树是平衡二叉树。
(2) 平衡二叉树的左子树和 右子树高度差不超过1,且左右子树都是平衡二叉树。
严格按此定义设计算法即可。
源码:(language:java)

public class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root==null)
            return true;
        else if(Math.abs(getHeight(root.left)-getHeight(root.right))<=1 && isBalanced(root.left) && isBalanced(root.right))
            return true;
        else
            return false;
    }
    private int getHeight(TreeNode root)
    {
        if(root==null)
            return 0;
        else
            return Math.max(getHeight(root.left),getHeight(root.right))+1;
    }
}

成绩:
2ms,beats 23.38%,众数2ms,64.97%

你可能感兴趣的:(Easy-题目24:110. Balanced Binary Tree)