-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution1305.java
48 lines (39 loc) · 1.19 KB
/
Solution1305.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
package leetcode.tree.bst;
import leetcode.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Solution1305 {
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
LinkedList<Integer> queue1 = new LinkedList<>();
LinkedList<Integer> queue2 = new LinkedList<>();
midOrder(root1, queue1);
midOrder(root2, queue2);
List<Integer> res = new ArrayList<>();
while (!queue1.isEmpty() || !queue2.isEmpty()) {
if (queue1.isEmpty()) {
res.add(queue2.poll());
continue;
}
if (queue2.isEmpty()) {
res.add(queue1.poll());
continue;
}
if (queue1.peek() <= queue2.peek()) {
res.add(queue1.poll());
} else {
res.add(queue2.poll());
}
}
return res;
}
private void midOrder(TreeNode node, Queue<Integer> queue) {
if (node == null) {
return;
}
midOrder(node.left, queue);
queue.offer(node.val);
midOrder(node.right, queue);
}
}