Open Shawngbk opened 7 years ago
different from maximum depth. if a node only has one child, the depth will be 1 + child depth.
public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; if(root.left == null) return minDepth(root.right) + 1; if(root.right == null) return minDepth(root.left) + 1; int lres = minDepth(root.left) + 1; int rres = minDepth(root.right) + 1; return lres < rres ? lres : rres; } }
different from maximum depth. if a node only has one child, the depth will be 1 + child depth.
public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; if(root.left == null) return minDepth(root.right) + 1; if(root.right == null) return minDepth(root.left) + 1; int lres = minDepth(root.left) + 1; int rres = minDepth(root.right) + 1; return lres < rres ? lres : rres; } }