Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Inorder Traversal

Treesmedium

Implement `inorder(root)` returning an array of values in inorder (left, node, right).

Sample tests

Input: inorder({"val":1,"left":{"val":2,"left":null,"right":null},"right":{"val":3,"left":null,"right":null}})
Output: [2,1,3]
Input: inorder(null)
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Visiting the node before the left subtree gives preorder, not inorder.

Learning resources

  • Wikipedia: Binary tree
Approach & explanation (try first)

Inorder visits left, node, right; concatenating those yields the sequence. O(n).

Loading...
⌘/Ctrl + Enter

Run your code to see results.