forked from anku580/Java-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecond_Minimum_In_Binary_Tree.java
More file actions
39 lines (36 loc) · 1.29 KB
/
Second_Minimum_In_Binary_Tree.java
File metadata and controls
39 lines (36 loc) · 1.29 KB
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
/*
This code has been written by - Amrit Raj
This is a functional code with the following specifications:-
Arguments - Root node of the Tree defined by class TreeNode
Return - An int signifying the second minimum node in the given tree. In case
it is not found, it returns -1
*/
public int findSecondMinimumValue(TreeNode root) {
//A queue to store the nodes..
Queue<TreeNode> q = new ArrayDeque<>();
if (root == null)
return -1;
int min = root.val;
int secondMin = Integer.MAX_VALUE;
boolean found = false;
q.add(root);
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode node = q.poll();
if (node.val > min) {
secondMin = Math.min(node.val, secondMin);
found = true;
continue;
}
//Left subtree..
if (node.left != null && node.left.val <= secondMin)
q.add(node.left);
//Right Subtree
if (node.right != null && node.right.val <= secondMin)
q.add(node.right);
}
}
//Returning second min, else -1...
return found? secondMin : -1;
}