Created
March 5, 2014 23:46
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 root) { | |
if(root == null) return 0; | |
int level = 1; | |
return minDeptRe(root, level); | |
} | |
public int minDeptRe(TreeNode root, int level){ | |
if(root.left == null && root.right == null) | |
return level; | |
int leftMinLevel=0; | |
int rightMinLevel=0; | |
/* if(root.left != null){//does not affect the running time | |
if(root.left.left == null && root.left.right == null) | |
return ++level; | |
} | |
if(root.right != null){ | |
if(root.right.left == null && root.right.right == null) | |
return ++level; | |
}*/ | |
if(root.left != null){ | |
leftMinLevel = minDeptRe(root.left, level+1); | |
} | |
if(root.right != null){ | |
rightMinLevel = minDeptRe(root.right, level+1); | |
} | |
if(leftMinLevel==0) return rightMinLevel; | |
if(rightMinLevel==0) return leftMinLevel; | |
return Math.min(leftMinLevel, rightMinLevel); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment