PPractick

DSA interview question

Which Traversal Pairs Determine a Unique Binary Tree?

Medium · Data structures · Topics: tree, theory

The problem

A backup tool stores binary trees (with distinct values) as two traversal sequences instead of pointers, and the team must pick which two traversals to store so that the tree can always be rebuilt unambiguously. Given two traversal types, coded 1 = preorder, 2 = inorder and 3 = postorder, return whether they always determine a unique tree. The rule is short: the pair must include inorder and one other traversal. Preorder or postorder identify the root; inorder then splits everything else into left and right. Preorder with postorder can't tell a lone left child from a lone right child.

Example: (1, 2) → true; (1, 3) → false.

Function signature

bool isPossible(int a, int b);  // 1 = preorder, 2 = inorder, 3 = postorder

Test cases

Input (a, b)OutputExplanation
(1, 2)truePreorder + inorder
(2, 3)trueInorder + postorder
(1, 3)false"1 with left child 2" and "1 with right child 2" give the same sequences
(2, 2)falseInorder alone doesn't fix the shape
(3, 1)falseOrder of the arguments doesn't matter

(Confirm: node values are distinct; "unique" means for every tree, not just some trees.)

How interviewers phrase it

Clarifying questions to ask

  1. Are the values distinct? (Yes. With duplicates, even pre + in can be ambiguous.)
  2. Are the two traversals of the same tree? (Yes.)
  3. Is a level-order traversal also allowed? (Not in this version; level order + inorder is also enough.)

Follow-ups to expect

  1. "Why does inorder matter?"
  2. "Why can't pre + post do it?"
  3. "How do you actually build from pre + in?"
  4. "And with post + in?"
  5. "Complexity of building?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in