Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. class Solution {
  2. private:
  3. int treeHeight(TreeNode * root, bool& balanced) {
  4. if (root == NULL) { balanced = true; return 0; }
  5. bool left, right;
  6. int left_height = treeHeight(root->left, left) + 1;
  7. int right_height = treeHeight(root->right, right) + 1;
  8. balanced = left && right;
  9. if (!balanced) return 0;
  10. int result = left_height - right_height;
  11. balanced = (result >= -1 && result <= 1);
  12. return max(left_height, right_height);
  13. }
  14. public:
  15. bool isBalanced(TreeNode *root) {
  16. if (root == NULL) return true;
  17. bool left, right;
  18. int result = treeHeight(root->left, left) - treeHeight(root->right, right);
  19. return (left&&right)&&(result >= -1 && result <= 1);
  20. }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement