(TreeNode root)
| 1 | class Solution { |
| 2 | public int maxDepth(TreeNode root) |
| 3 | { |
| 4 | // Base Case |
| 5 | if(root==null) return 0; |
| 6 | // recursion calls |
| 7 | int lh=maxDepth(root.left); |
| 8 | int rh=maxDepth(root.right); |
| 9 | // non favourable |
| 10 | if(lh==-1 || rh==-1) return -1; |
| 11 | if(Math.abs(lh-rh)>1) return -1; |
| 12 | // depth of the tree |
| 13 | return 1+Math.max(lh,rh); |
| 14 | } |
| 15 | |
| 16 | public boolean isBalanced(TreeNode root) { |
| 17 | if(maxDepth(root)==-1) return false; |