Advertisement
jibha

Untitled

Jan 30th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14.  
  15. vector<int> ans;
  16.  
  17. void DFS(TreeNode* root){
  18.  
  19. if(root==nullptr){
  20. return;
  21. }
  22.  
  23. ans.push_back(root->val);
  24.  
  25. DFS(root->left);
  26. DFS(root->right);
  27.  
  28. return;
  29.  
  30. }
  31.  
  32. vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
  33.  
  34. DFS(root1);
  35. DFS(root2);
  36.  
  37. sort(ans.begin(),ans.end());
  38.  
  39. return ans;
  40.  
  41. }
  42. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement