-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoBST.java
More file actions
148 lines (113 loc) · 3.6 KB
/
MergeTwoBST.java
File metadata and controls
148 lines (113 loc) · 3.6 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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.util.ArrayList;
import java.util.List;
/**
* Leet code problem, #1305: All Elements in Two Binary Search Trees
* https://leetcode.com/problems/all-elements-in-two-binary-search-trees/
*/
public class MergeTwoBST {
public static void main(String[] args) {
int arr1[] = { 1, 3, 5, 7 };
Node root1 = BinarySearchTree.sortedArrayToBST(arr1, 0, arr1.length - 1);
int arr2[] = { 2, 4, 6, 8, 10, 12, 14 };
Node root2 = BinarySearchTree.sortedArrayToBST(arr2, 0, arr2.length - 1);
List<Integer> result = getAllElelments(root1, root2);
System.out.println(result);
}
public static List<Integer> getAllElelments(Node root1, Node root2) {
List<Integer> res1 = new ArrayList<>();
storeInOrderTraversal(root1, res1);
List<Integer> res2 = new ArrayList<>();
storeInOrderTraversal(root2, res2);
return mergeTwoSortedArrays(res1, res2);
}
public static List<Integer> mergeTwoSortedArrays(List<Integer> res1, List<Integer> res2) {
List<Integer> res = new ArrayList<>();
int i = 0;
int j = 0;
int n1 = res1.size();
int n2 = res2.size();
while (i < n1 && j < n2) {
if (res1.get(i) < res2.get(j)) {
res.add(res1.get(i));
i++;
} else {
res.add(res2.get(j));
j++;
}
}
while (i < n1) {
res.add(res1.get(i));
i++;
}
while (j < n2) {
res.add(res2.get(j));
j++;
}
return res;
}
public static void storeInOrderTraversal(Node node, List<Integer> res) {
if (node == null) {
return;
}
storeInOrderTraversal(node.left, res);
res.add(node.val);
storeInOrderTraversal(node.right, res);
}
static class BinarySearchTree {
public Node root;
public Node search(int target) {
return search(this.root, target);
}
public static Node search(Node root, int target) {
if (root == null) {
return null;
}
if (root.val == target) {
return root;
}
if (root.val > target) {
return search(root.left, target);
}
return search(root.right, target);
}
public void sortedArrayToBST(int[] arr) {
this.root = sortedArrayToBST(arr, 0, arr.length - 1);
}
public static Node sortedArrayToBST(int[] arr, int start, int end) {
if (start > end) {
return null;
}
int mid = start + (end - start) / 2;
Node node = new Node(arr[mid]);
node.left = sortedArrayToBST(arr, start, mid - 1);
node.right = sortedArrayToBST(arr, mid + 1, end);
return node;
}
public void printInorderTraversal() {
printInorderTraversal(this.root);
}
public static void printInorderTraversal(Node node) {
if (node == null) {
return;
}
printInorderTraversal(node.left);
System.out.print(node.val + " ");
printInorderTraversal(node.right);
}
}
static class Node {
int val;
Node left;
Node right;
Node() {
}
Node(int val) {
this.val = val;
}
Node(int x, Node left, Node right) {
this.val = x;
this.left = left;
this.right = right;
}
}
}