PPractick

DSA interview question

Binary Tree Inorder Traversal

Easy · Data structures · Topics: tree, dfs, recursion

The problem

A contacts app stores names in a binary search tree, and its "show all, sorted" button walks the tree inorder: left subtree, then the node, then the right subtree. For a BST that visits the keys in sorted order, and for any binary tree it gives the natural "left to right" reading of the nodes. The recursive version mirrors the definition directly.

Example: [1, null, 2, 3] → [1, 3, 2].

Function signature

vector<int> inorderTraversal(TreeNode* root);

Test cases

Input (level order)OutputExplanation
[1, null, 2, 3][1, 3, 2]
[1, 2, 3, 4, 5, null, 8, null, null, 6, 7, 9][4, 2, 6, 5, 7, 1, 3, 9, 8]
[2, 1, 3][1, 2, 3]A BST comes out sorted
[][]
[1][1]

(Confirm: LeetCode's level-order input format; the tree may be empty.)

How interviewers phrase it

Clarifying questions to ask

  1. Recursive or iterative?
  2. Can the tree be empty or very deep?
  3. Is it a BST? (Then inorder means sorted order, which many BST problems rely on.)

Follow-ups to expect

  1. "Why does inorder sort a BST?"
  2. "Iterative version?"
  3. "Morris inorder?"
  4. "Kth smallest in a BST?"
  5. "Complexity?"

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