Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 12, 2017 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InterviewBytes/70c9632048b93b99fa8c4d498cf11541 to your computer and use it in GitHub Desktop.
Save InterviewBytes/70c9632048b93b99fa8c4d498cf11541 to your computer and use it in GitHub Desktop.
MinDepth of a tree
package com.interviewbytes.trees;
public class MinDepth {
public int minDepth(TreeNode root) {
if (root == null) return 0;
if (root.left == null && root.right == null) return 1;
if (root.left == null || root.right == null) return 1 + minDepth(root.left) + minDepth(root.right);
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
}
package com.interviewbytes.trees;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment