forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_671.java
39 lines (35 loc) · 1022 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
32
33
34
35
36
37
38
39
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) {
if (root == null) {
return -1;
}
Set<Integer> set = new TreeSet<>();
dfs(root, set);
Iterator<Integer> iterator = set.iterator();
int count = 0;
while (iterator.hasNext()) {
count++;
int result = iterator.next();
if (count == 2) {
return result;
}
}
return -1;
}
private void dfs(TreeNode root, Set<Integer> set) {
if (root == null) {
return;
}
set.add(root.val);
dfs(root.left, set);
dfs(root.right, set);
return;
}
}
}