/** * 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 flag = 1; depth(root,0,flag); if(flag) return true; else return false; } int depth(TreeNode* root,int cnt,int& flag){ if(root == NULL) return cnt; int a = depth(root->left,cnt+1,flag); int b = depth(root->right,cnt+1,flag); if(abs(a-b) > 1) flag = 0; return max(a,b); } };
_