Balanced Binary Tree

 

    bool isBalanced(TreeNode *root) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        int depth;
        return helper(root,depth);
    }
    
    bool helper(TreeNode* root,int& depth)
    {
        if(!root)
        {
            depth = 0;
            return true;
        }
        int ld,rd;
        bool bBalance = helper(root->left,ld)&&helper(root->right,rd);
        if(abs(ld-rd)>1)
            bBalance = false;
        depth = max(ld,rd)+1;
        return bBalance;
        
    }

  

posted @ 2013-10-04 23:00  summer_zhou  阅读(106)  评论(0编辑  收藏  举报