Skip to content

Instantly share code, notes, and snippets.

@riyafa
Last active September 16, 2022 07:55
Show Gist options
  • Save riyafa/bbbc3aa1e6e15b8da6d15679894f9a1a to your computer and use it in GitHub Desktop.
Save riyafa/bbbc3aa1e6e15b8da6d15679894f9a1a to your computer and use it in GitHub Desktop.
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