public static int MinDepthV2(LeetCode111TreeNode root) { if (root == null) { return(0); } if (root.left == null) { return(1 + MinDepthV2(root.right)); } if (root.right == null) { return(1 + MinDepthV2(root.left)); } return(1 + Math.Min(MinDepthV2(root.left), MinDepthV2(root.right))); }
public static int MinDepth(LeetCode111TreeNode root) { if (root == null) { return(0); } if (root.left == null && root.right == null) { return(1); } var minDepth = int.MaxValue; if (root.left != null) { minDepth = Math.Min(MinDepth(root.left), minDepth); } if (root.right != null) { minDepth = Math.Min(MinDepth(root.right), minDepth); } return(minDepth + 1); }
public LeetCode111TreeNode(int val = 0, LeetCode111TreeNode left = null, LeetCode111TreeNode right = null) { this.val = val; this.left = left; this.right = right; }