/
allElementsInBinaryTree.cpp
49 lines (41 loc) · 1.14 KB
/
allElementsInBinaryTree.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
// 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) {}
};
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
void getList(vector<int> &ret,TreeNode* root){
if(root->right!=NULL)
{
ret.push_back(root->right->val);
getList(ret,root->right);
}
if(root->left!=NULL)
{
ret.push_back(root->left->val);
getList(ret,root->left);
}
return;
}
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> ret;
if(root1){
ret.push_back(root1->val);
getList(ret,root1);
}
if(root2){
ret.push_back(root2->val);
getList(ret,root2);
}
std::sort(ret.begin(),ret.end());
return ret;
}
};