forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_671.java
31 lines (26 loc) · 758 Bytes
/
_671.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class _671 {
public static class Solution1 {
public int findSecondMinimumValue(TreeNode root) {
TreeSet<Integer> set = new TreeSet<>();
dfs(root, set);
if (set.size() < 2) {
return -1;
}
set.pollFirst();
return set.pollFirst();
}
private void dfs(TreeNode root, TreeSet<Integer> set) {
if (root == null) {
return;
}
set.add(root.val);
dfs(root.left, set);
dfs(root.right, set);
}
}
}