MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / maxDepth

Method maxDepth

TreeBalancedBinaryTree.java:2–14  ·  view source on GitHub ↗
(TreeNode root)

Source from the content-addressed store, hash-verified

1class 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;

Callers 1

isBalancedMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected