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.
1import java.util.ArrayList;
2import java.util.List;
3
4public class Solution {
5 public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
6 List<Integer> list1 = new ArrayList<>();
7 List<Integer> list2 = new ArrayList<>();
8 inOrder(root1, list1);
9 inOrder(root2, list2);
10 return mergeSortedLists(list1, list2);
11 }
12
13 private void inOrder(TreeNode root, List<Integer> list) {
14 if (root == null) return;
15 inOrder(root.left, list);
16 list.add(root.val);
17 inOrder(root.right, list);
18 }
19
20 private List<Integer> mergeSortedLists(List<Integer> list1, List<Integer> list2) {
21 List<Integer> merged = new ArrayList<>();
22 int i = 0, j = 0;
23 while (i < list1.size() && j < list2.size()) {
24 if (list1.get(i) < list2.get(j))
25 merged.add(list1.get(i++));
26 else
27 merged.add(list2.get(j++));
28 }
29 while (i < list1.size())
30 merged.add(list1.get(i++));
31 while (j < list2.size())
32 merged.add(list2.get(j++));
33 return merged;
34 }
35
36 public static class TreeNode {
37 int val;
38 TreeNode left;
39 TreeNode right;
40 TreeNode(int x) { val = x; }
41 }
42}
43
This Java solution performs in-order traversal of both trees and stores the results in two lists. These sorted lists are merged into a final sorted list which is returned. This approach efficiently combines the elements with minimal additional space usage.
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.
1using System.Collections.Generic;
2
3public class TreeNode {
4 public int val;
5 public TreeNode left;
6 public TreeNode right;
7 public TreeNode(int x) { val = x; }
8}
9
10public class Solution {
11 public IList<int> GetAllElements(TreeNode root1, TreeNode root2) {
12 List<int> result = new List<int>();
13 Stack<TreeNode> stack1 = new Stack<TreeNode>();
14 Stack<TreeNode> stack2 = new Stack<TreeNode>();
15 while (root1 != null || root2 != null || stack1.Count > 0 || stack2.Count > 0) {
16 while (root1 != null) {
17 stack1.Push(root1);
18 root1 = root1.left;
19 }
20 while (root2 != null) {
21 stack2.Push(root2);
22 root2 = root2.left;
23 }
24 if (stack2.Count == 0 || (stack1.Count > 0 && stack1.Peek().val <= stack2.Peek().val)) {
25 root1 = stack1.Pop();
26 result.Add(root1.val);
27 root1 = root1.right;
28 } else {
29 root2 = stack2.Pop();
30 result.Add(root2.val);
31 root2 = root2.right;
32 }
33 }
34 return result;
35 }
36}
37
C#'s iterative view captures dual stack-based tree traversals, sequencing iterative descent with staple command efficiency, processing nodes just-in-time. This arrangement not only reduces execution stack concerns but channels flexibility in traversal roles between both trees, optimizing per-turn checks and operations.