leetcode 110. Balanced Binary Tree

 

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        int depth = 0;
        return Balanced(root,depth);
    }
    bool Balanced(TreeNode* root,int& depth){
        if(root == NULL){
            depth = 0;
            return true;
        }
        int left,right;
        if(Balanced(root->left,left) && Balanced(root->right,right)){
            int gap = left - right;
            if(gap <= 1 && gap >= -1){
                depth = left > right ? left + 1 : right + 1;
                return true;
            }
        }
        return false;
    }
};

 

posted @ 2019-04-07 15:55  有梦就要去实现他  阅读(155)  评论(0编辑  收藏  举报