-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllElementsinTwoBinarySearchTrees_1305.cpp
58 lines (48 loc) · 1.63 KB
/
AllElementsinTwoBinarySearchTrees_1305.cpp
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
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 1305. All Elements in Two Binary Search Trees
~ Link : https://leetcode.com/problems/all-elements-in-two-binary-search-trees/
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void getAllNodeValues(TreeNode *root, vector<int> &values) {
if (!root)
return;
getAllNodeValues(root -> left, values);
values.emplace_back(root -> val);
getAllNodeValues(root -> right, values);
}
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> res, values1, values2;
getAllNodeValues(root1, values1);
getAllNodeValues(root2, values2);
int n = values1.size(), index1 = 0;
int m = values2.size(), index2 = 0;
while (index1 < n && index2 < m) {
if (values1[index1] <= values2[index2])
res.emplace_back(values1[index1++]);
else
res.emplace_back(values2[index2++]);
}
while (index1 < n) {
res.emplace_back(values1[index1++]);
}
while (index2 < m) {
res.emplace_back(values2[index2++]);
}
return res;
}
};
// Time Complexity - O(n + m)
// Auxiliary Space - O(n + m)