Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean isBalanced(TreeNode root) {
- return height(root) >= 0;
- }
- int height(TreeNode root) {
- if (root == null)
- return 0;
- int l, r;
- l = height(root.left);
- if (l == -1)
- return -1;
- r = height(root.right);
- if (r == -1)
- return -1;
- if (Math.abs(l-r) > 1) {
- return -1;
- } else {
- return Math.max(l, r) + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement