Check if Two Trees are Identical

Data Structures
Easy
Spotify
67.5K views

Given the roots of two binary trees, determine if they are structurally identical and if the nodes have the same values. Implement using DFS (recursion).

Why Interviewers Ask This

Spotify engineers value code clarity and robustness in their streaming infrastructure. This question tests your ability to handle recursive logic, manage base cases for null pointers, and verify structural integrity alongside data values. It reveals if you can translate a conceptual definition of identity into clean, bug-free traversal code without over-engineering the solution.

How to Answer This Question

1. Clarify requirements: Confirm that identical trees must match both structure (shape) and node values, not just contain the same set of numbers. 2. Define the base cases first: Explain that two null nodes are identical, but one null and one non-null node are not. 3. Propose the recursive strategy: State that you will compare current node values, then recursively check left subtrees and right subtrees. 4. Draft the logic flow: Walk through the condition where if both nodes exist and have equal values, you proceed to children; otherwise, return false immediately. 5. Analyze complexity: Briefly mention O(n) time and O(h) space complexity due to recursion stack depth. 6. Optimize communication: Use Spotify's collaborative style by asking about edge cases like empty trees before writing code.

Key Points to Cover

  • Explicitly handling null pointer edge cases before accessing node values
  • Demonstrating understanding that structure matters as much as data values
  • Correctly implementing the logical AND operation between left and right recursive calls
  • Articulating O(n) time complexity based on visiting each node once
  • Acknowledging O(h) space complexity related to the call stack depth

Sample Answer

To solve this efficiently, I would use a Depth-First Search approach with recursion. First, I need to establish clear base cases. If both input roots are null, the trees are identical up to this point, so I return true. However, if only one of them is null while the other exists, the structures differ, so I return false immediately. Next, I check if the values of the current nodes match. If they do not, the trees are not identical, and I return false. If the values match, I must ensure the entire subtree structure is the same. I do this by recursively calling the function on the left children and the right children of both trees. The result is true only if both recursive calls return true. For example, consider two trees where the root is 1, left child is 2, and right child is 3. If the second tree has the same structure and values, my function compares roots, then recurses to compare left nodes (2 vs 2), then right nodes (3 vs 3). Since all checks pass, it returns true. This approach ensures we visit every node exactly once, giving us O(n) time complexity. Regarding space, the recursion stack goes as deep as the height of the tree, resulting in O(h) space complexity, which is optimal for this problem.

Common Mistakes to Avoid

  • Comparing node values without checking if both nodes exist first, causing null pointer exceptions
  • Returning true if only the root values match without traversing the rest of the tree
  • Using iteration instead of the requested DFS recursion, missing the opportunity to show recursive thinking
  • Failing to explain why the order of checks (null check, then value check) is critical for safety

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 30 Spotify questions