Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public boolean isBalanced(TreeNode root) {
- int g=b(root);
- if(g==-1)
- return false;
- return true;
- }
- public static int b(TreeNode root)
- {
- if(root==null)
- return 0;
- int l_h=b(root.left);
- if(l_h==-1)
- return -1;
- int r_h=b(root.right);
- if(r_h==-1)
- return -1;
- if(Math.abs(r_h-l_h)>1)
- return -1;
- else
- return (1+Math.max(l_h,r_h));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment