-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindSecondMinimumValue.java
46 lines (43 loc) · 1.2 KB
/
FindSecondMinimumValue.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
40
41
42
43
44
45
46
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class FindSecondMinimumValue {
/*
Runtime: 1 ms, faster than 27.86% of Java online submissions for Second Minimum Node In a Binary Tree.
Memory Usage: 41.5 MB, less than 50.10% of Java online submissions for Second Minimum Node In a Binary Tree.
*/
public int findSecondMinimumValue(TreeNode root) {
List<Integer> list=new ArrayList<>();
if(root==null) return -1;
helper(root,list);
Collections.sort(list);
int v=list.get(0);
for(int i=1;i<list.size();i++)
{
if(list.get(i)>v)
{
return list.get(i);
}
}
return -1;
}
public void helper(TreeNode root,List<Integer> list)
{
if(root==null) return;
list.add(root.val);
helper(root.left,list);
helper(root.right,list);
}
}