This approach leverages the in-order traversal property of Binary Search Trees (BSTs), which yields elements in sorted order. We perform an in-order traversal on both trees to extract elements into separate lists. We then merge these two sorted lists into one sorted list.
Time Complexity: O(n + m) where n and m are the number of nodes in root1 and root2, respectively. Space Complexity: O(n + m) for storing the elements from both trees.
1using System;
2using System.Collections.Generic;
3
4public class TreeNode {
5 public int val;
6 public TreeNode left;
7 public TreeNode right;
8 public TreeNode(int x) { val = x; }
9}
10
11public class Solution {
12 public IList<int> GetAllElements(TreeNode root1, TreeNode root2) {
13 List<int> list1 = new List<int>();
14 List<int> list2 = new List<int>();
15 InOrder(root1, list1);
16 InOrder(root2, list2);
17 return MergeSortedLists(list1, list2);
18 }
19
20 private void InOrder(TreeNode node, List<int> list) {
21 if (node == null) return;
22 InOrder(node.left, list);
23 list.Add(node.val);
24 InOrder(node.right, list);
25 }
26
27 private List<int> MergeSortedLists(List<int> list1, List<int> list2) {
28 List<int> merged = new List<int>();
29 int i = 0, j = 0;
30 while (i < list1.Count && j < list2.Count) {
31 if (list1[i] < list2[j])
32 merged.Add(list1[i++]);
33 else
34 merged.Add(list2[j++]);
35 }
36 while (i < list1.Count)
37 merged.Add(list1[i++]);
38 while (j < list2.Count)
39 merged.Add(list2[j++]);
40 return merged;
41 }
42}
43
C# solution employs in-order traversal to collect elements from two binary search trees into lists. These lists are then merged into one sorted list which is returned to the caller.
This approach uses stacks to perform an iterative in-order traversal of both trees simultaneously. We make use of two separate stacks to store the nodes of current branches of the trees. The smallest node from the top of the stacks is chosen and processed to build the result list in sorted order.
Time Complexity: O(n + m) for processing all nodes, where n and m are the numbers of nodes in the two trees. Space Complexity: O(h1 + h2) where h1 and h2 are the heights of the two trees for the stack size.
1#include <vector>
2#include <stack>
3using namespace std;
4
5struct TreeNode {
6 int val;
7 TreeNode *left;
8 TreeNode *right;
9 TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10};
11
12vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
13 vector<int> result;
14 stack<TreeNode*> s1, s2;
15 while (root1 || root2 || !s1.empty() || !s2.empty()) {
16 while (root1) {
17 s1.push(root1);
18 root1 = root1->left;
19 }
20 while (root2) {
21 s2.push(root2);
22 root2 = root2->left;
23 }
24
25 if (s2.empty() || (!s1.empty() && s1.top()->val <= s2.top()->val)) {
26 root1 = s1.top(); s1.pop();
27 result.push_back(root1->val);
28 root1 = root1->right;
29 } else {
30 root2 = s2.top(); s2.pop();
31 result.push_back(root2->val);
32 root2 = root2->right;
33 }
34 }
35 return result;
36}
37
The C++ solution illustrates an iterative in-order traversal with two stacks. Each tree's nodes are pushed onto its respective stack, only to pop and process the smallest. This effectively orders nodes by value, producing a merged sorted sequence, minimizing space usage to maintain stacks only as deep as the trees' heights.