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;
    }
};
原文地址:https://www.cnblogs.com/ymjyqsx/p/10665686.html