class Solution{ public: int height(Node * temp) { if(temp==NULL) return 0; int lh=height(temp->left); int rh=height(temp->right); return 1+max(lh,rh); } //Function to check whether a binary tree is balanced or not. bool isBalanced(Node *root) { if(root==NULL) return 1; int lh=height(root->left); int rh=height(root->right); if(abs(lh-rh)<=1 && isBalanced(root->left) && isBalanced(root->right)) return 1; return 0; // Your Code here } };
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