public static int Helper(LeetCode671TreeNode root, int min) { if (root.val != min) { return(root.val); } if (root.left == null) { return(-1); } var left = Helper(root.left, root.val); var right = Helper(root.right, root.val); if (left == -1) { return(right); } if (right == -1) { return(left); } return(Math.Min(left, right)); }
public static int FindSecondMinimumValue(LeetCode671TreeNode root) { if (root == null || root.left == null) { return(-1); } var left = Helper(root.left, root.val); var right = Helper(root.right, root.val); if (left == -1) { return(right); } if (right == -1) { return(left); } return(Math.Min(left, right)); }
public static int FindSecondMinimumValueV2(LeetCode671TreeNode root) { return(Dfs(root, root.val)); }
public LeetCode671TreeNode(int val = 0, LeetCode671TreeNode left = null, LeetCode671TreeNode right = null) { this.val = val; this.left = left; this.right = right; }