-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1305. All Elements in Two Binary Search Trees.java
69 lines (69 loc) · 1.81 KB
/
1305. All Elements in Two Binary Search Trees.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
68
69
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
//don't edit this code
public static int countnodes(TreeNode root){
if (root == null) {
return 0;
}
return 1 + countnodes(root.left) + countnodes(root.right);
}
public static int inorder(int[] arr, int index, TreeNode root) {
if (root == null) {
return index;
}
index = inorder(arr, index, root.left);
// Prevent ArrayIndexOutOfBoundsException
arr[index] = root.val;
index++;
index = inorder(arr, index, root.right);
return index;
}
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
List<Integer> list=new ArrayList<>();
int root1n=countnodes(root1);
int[] r1=new int[root1n];
int root2n=countnodes(root2);
int[] r2=new int[root2n];
inorder(r1,0,root1);
inorder(r2,0,root2);
int i=0;
int j=0;
while(i<r1.length && j<r2.length){
if(r1[i]<r2[j]){
list.add(r1[i]);
i++;
}else if(r1[i]>r2[j]){
list.add(r2[j]);
j++;
}else{
list.add(r1[i]);
list.add(r2[j]);
i++;
j++;
}
}
while(i<r1.length){
list.add(r1[i]);
i++;
}
while(j<r2.length){
list.add(r2[j]);
j++;
}
return list;
}
}