Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c4321ef

Browse files
committedNov 17, 2021
Updates second minimum node in binary tree
1 parent 3e2e46d commit c4321ef

File tree

1 file changed

+1
-9
lines changed

1 file changed

+1
-9
lines changed
 

‎src/SecondMinimumNodeInBinaryTree.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class SecondMinimumNodeInBinaryTree {
77
public int findSecondMinimumValue(TreeNode root) {
88
Set<Integer> elements = treeToSet(root);
99
if (elements.size() < 2) return -1;
10-
Queue<Integer> queue = setToMinHeap(elements);
10+
Queue<Integer> queue = new PriorityQueue<>(elements);
1111
queue.poll();
1212
return queue.peek();
1313
}
@@ -24,12 +24,4 @@ private static void addTreeNodesToSet(TreeNode root, Set<Integer> set) {
2424
addTreeNodesToSet(root.left, set);
2525
addTreeNodesToSet(root.right, set);
2626
}
27-
28-
private static Queue<Integer> setToMinHeap(Set<Integer> elements) {
29-
Queue<Integer> queue = new PriorityQueue<>();
30-
for (int element : elements) {
31-
queue.add(element);
32-
}
33-
return queue;
34-
}
3527
}

0 commit comments

Comments
 (0)
Please sign in to comment.