DSA interview question
A database iterates the keys of a huge binary search tree in sorted (inorder) order on a server where every byte of stack counts. Morris inorder traversal uses no stack and no recursion. It temporarily threads each node's inorder predecessor back to the node and walks back along the thread after finishing the left subtree. The node is recorded when the thread is removed, because that's the moment its left subtree is complete.
Example: [5, 3, 7, 2, 4, 6, 8] → [2, 3, 4, 5, 6, 7, 8].
vector<int> morrisInorder(TreeNode* root); // O(1) extra space; the tree is restored
| Input (level order) | Output | Explanation |
|---|---|---|
[1, 2, 3, 4, 5, 6, 7] | [4, 2, 5, 1, 6, 3, 7] | |
[1, null, 2, 3] | [1, 3, 2] | |
[5, 3, 7, 2, 4, 6, 8] | [2, 3, 4, 5, 6, 7, 8] | A BST comes out sorted |
[1] | [1] | |
[] | [] |
(Confirm: no stack and no recursion; the tree must be unchanged afterwards.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in