Leetcode 110. 平衡二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root,int &h){//h是高度
        if(!root) {h=0;return true;}
        int hl,hr;
        return isBalanced(root->left,hl) && isBalanced(root->right,hr) && (h=max(hl+1,hr+1),abs(hl-hr)<=1);
    }
    bool isBalanced(TreeNode* root) {
        int h;
        return isBalanced(root,h);
    }
};

你可能感兴趣的:(Leetcode 110. 平衡二叉树)