Skip to content

Latest commit

 

History

History
31 lines (28 loc) · 794 Bytes

File metadata and controls

31 lines (28 loc) · 794 Bytes
title 110-balanced-binary-tree
tags 在河之洲,算法,小书匠
grammar_cjkRuby true

problem

110-balanced-binary-tree

solution

递归法

    bool isBalanced(TreeNode* root) {
        if (helper(root) == -1)
            return false;
        return true;
    }
    int helper(TreeNode *root)
    {
        if (root == NULL)
            return 0;
        int leftDepth  = helper(root->left );
        int rightDepth = helper(root->right);
        if (leftDepth == -1 || rightDepth == -1)
            return -1;
        if (leftDepth == rightDepth || rightDepth == leftDepth+1 || leftDepth == rightDepth+1 )
            return leftDepth>rightDepth?leftDepth+1:rightDepth+1;
        return -1;
    }