class Solution { int flag; public: int height(TreeNode* root) { if(root==NULL)return 0; int hl = height(root->left); int hr = height(root->right); if(abs(hl-hr)>1)flag=1; return 1+max(hl,hr); } bool isBalanced(TreeNode* root) { int h = height(root); if(flag)return false; else return true; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter