1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
class Solution { public: bool isBalanced(TreeNode* root) { check(root); return ret; } private: bool ret = true; int check(TreeNode* root) { if(!root || !ret) return 0; auto left = check(root->left); auto right = check(root->right); if(abs(left -right) > 1) ret = false; return max(left, right) + 1; } };
|