Created
August 10, 2019 15:21
-
-
Save shharma-vipin/5ec413db09c685d8c3af065f77f2738b to your computer and use it in GitHub Desktop.
Minimum Depth Of Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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