Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This function should return tree if passed tree
- // is balanced, else false.
- bool Balanced(Node*root,int *height)
- {
- int lh = 0, rh = 0;
- int l = 0, r = 0;
- if (root == NULL) {
- *height = 0;
- return 1;
- }
- l = Balanced(root->left, &lh);
- r = Balanced(root->right, &rh);
- *height = (lh > rh ? lh : rh) + 1;
- if (abs(lh -rh) >= 2)
- return 0;
- else
- return l&&r;
- }
- bool isBalanced(Node *root)
- {
- int height=0;
- return Balanced(root,&height);
- }
Add Comment
Please, Sign In to add comment