LeetCode 1305: All Elements in Two Binary Search Trees Solution

Master LeetCode problem 1305 (All Elements in Two Binary Search Trees), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1305. All Elements in Two Binary Search Trees

Problem Explanation

Explanation:

To solve this problem, we can perform an inorder traversal on both binary search trees simultaneously. As we traverse the trees in inorder, we can merge the results into a single sorted list. We can achieve this by using two stacks to perform iterative inorder traversal on each tree.

  • Algorithmic Idea:

    1. Initialize two stacks for each tree and an empty list to store the merged result.
    2. Perform iterative inorder traversal on both trees simultaneously.
    3. Compare the elements at the top of the two stacks and add the smaller element to the result list.
    4. Pop the element from the corresponding stack and move to its right subtree if it exists.
    5. Repeat steps 3 and 4 until both stacks are empty.
    6. Return the merged sorted list.
  • Time Complexity: O(m + n) where m and n are the number of nodes in the two trees.

  • Space Complexity: O(m + n) for the two stacks and the merged list. :

Solution Code

class Solution {
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack();
        
        while (root1 != null || root2 != null || !stack1.isEmpty() || !stack2.isEmpty()) {
            while (root1 != null) {
                stack1.push(root1);
                root1 = root1.left;
            }
            while (root2 != null) {
                stack2.push(root2);
                root2 = root2.left;
            }
            
            if (stack2.isEmpty() || (!stack1.isEmpty() && stack1.peek().val <= stack2.peek().val)) {
                TreeNode node = stack1.pop();
                result.add(node.val);
                root1 = node.right;
            } else {
                TreeNode node = stack2.pop();
                result.add(node.val);
                root2 = node.right;
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1305 (All Elements in Two Binary Search Trees)?

This page provides optimized solutions for LeetCode problem 1305 (All Elements in Two Binary Search Trees) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1305 (All Elements in Two Binary Search Trees)?

The time complexity for LeetCode 1305 (All Elements in Two Binary Search Trees) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1305 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1305 in Java, C++, or Python.

Back to LeetCode Solutions