Last active
September 16, 2022 07:55
-
-
Save riyafa/bbbc3aa1e6e15b8da6d15679894f9a1a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class MinimumDepthBinaryTreeDFS { | |
public int minDepth(TreeNode root) { | |
if(root == null) { | |
return 0; | |
} | |
int leftHeight = minDepth(root.left); | |
int rightHeight = minDepth(root.right); | |
if(leftHeight == 0 || rightHeight == 0) { | |
return leftHeight + rightHeight + 1; | |
} | |
return Math.min(leftHeight, rightHeight) + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment