Skip to content

Instantly share code, notes, and snippets.

@shharma-vipin
Created August 10, 2019 15:21
Show Gist options
  • Save shharma-vipin/5ec413db09c685d8c3af065f77f2738b to your computer and use it in GitHub Desktop.
Save shharma-vipin/5ec413db09c685d8c3af065f77f2738b to your computer and use it in GitHub Desktop.
Minimum Depth Of Tree
public int minDepth(TreeNode A) {
if(A== null){
return 0;
}
if(A.left == null && A.right == null){
return 1;
}
if(A.left == null){
return minDepth(A.right)+1;
}
if(A.right == null){
return minDepth(A.left)+1;
}
return Math.min(minDepth(A.right),minDepth(A.left))+1;
}
public int minimumDepth(TreeNode root){
return utility(root,0);
}
private int utility(TreeNode n, int level){
if(n == null) return level;
level++;
return Math.min(utility(n.left,level),utility(n.right,level);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment