Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 900 Bytes

20200929.md

File metadata and controls

42 lines (30 loc) · 900 Bytes

Algorithm

剑指offer-平衡二叉树

Description

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

在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树

Solution

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 left>right?left+1:right+1;
    }
}

Discuss

Review

Tip

Share