-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1305.两棵二叉搜索树中的所有元素.cpp
73 lines (71 loc) · 1.87 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* @lc app=leetcode.cn id=1305 lang=cpp
*
* [1305] 两棵二叉搜索树中的所有元素
*/
#include <iostream>
#include <vector>
#include <iterator>
// #include <>
using namespace std;
template <class T> std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
if (!v.empty()) {
out << '[';
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
out << "]";
}
return out;
}
// @lc code=start
/**
* 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 {
private:
public:
// 64 39;
// 略low
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> ret;
vector<int> nums1;
vector<int> nums2;
midOrder(root1, nums1);
midOrder(root2, nums2);
int i = 0, j = 0;
while(i < nums1.size() && j < nums2.size()){
if(nums1[i] <= nums2[j]){
ret.push_back(nums1[i++]);
}else{
ret.push_back(nums2[j++]);
}
}
while(i < nums1.size()){
ret.push_back(nums1[i++]);
}
while(j < nums2.size()){
ret.push_back(nums2[j++]);
}
return ret;
}
void midOrder(TreeNode* root, vector<int>& nums){
if(!root){
return;
}
if(root->left){
midOrder(root->left, nums);
}
nums.push_back(root->val);
if(root->right){
midOrder(root->right, nums);
}
}
};
// @lc code=end