-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMergeTwoBSTs.java
67 lines (64 loc) · 1.63 KB
/
MergeTwoBSTs.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*https://practice.geeksforgeeks.org/problems/merge-two-bst-s/1*/
//converting into DLL approach
class Solution
{
public Node head1, tail1, head2, tail2;
public void addNode(int data)
{
tail1.right = new Node(data);
tail1.right.left = tail1;
tail1 = tail1.right;
}
public List<Integer> merge(Node root1,Node root2)
{
head1 = new Node(0);
tail1 = head1;
//convert both the trees to DLLs
traverseInOrder(root2);
root2 = null;
head2 = head1.right;
head2.left = null;
tail2 = tail1;
head1 = new Node(0);
tail1 = head1;
traverseInOrder(root1);
head1 = head1.right;
head1.left = null;
root1 = null;
//Merge two DLLs
List<Integer> result = new ArrayList<Integer>();
while (head1 != null && head2 != null)
{
if (head1.data <= head2.data)
{
result.add(head1.data);
head1 = head1.right;
}
else
{
result.add(head2.data);
head2 = head2.right;
}
}
while (head1 != null)
{
result.add(head1.data);
head1 = head1.right;
}
while (head2 != null)
{
result.add(head2.data);
head2 = head2.right;
}
return result;
}
public void traverseInOrder(Node root)
{
if (root != null)
{
traverseInOrder(root.left);
addNode(root.data);
traverseInOrder(root.right);
}
}
}