Leetcode Problem 1305. All Elements in Two Binary Search Trees
1305. All Elements in Two Binary Search Trees
Leetcode Solutions
Iterative Inorder Traversal, One Pass, Linear Time
Initialize two stacks to keep track of the nodes for both trees.
Initialize an empty list to store the merged output.
Perform a loop until both stacks are empty.
a. Push all the left children of the current node of both trees onto their respective stacks.
b. Compare the top elements of both stacks.
c. Pop the smaller element from its stack and add it to the output list.
d. Move to the right child of the popped node and continue the process.