Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

preorder =
inorder =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Preorder gives root-first order; inorder splits left/right subtrees around the root.

Approach

Use a hash map from value to its inorder index. Walk preorder with a pointer pre_i. Recursively build subtrees from inorder ranges.

Why it works

Each root chosen from preorder partitions the inorder range into exact left and right subtree elements.

Code
Loading...
Complexity
Time: O(n)
Space: O(n)
Solution
Updated: 2026-02-23

Idea

Preorder gives root-first order; inorder splits left/right subtrees around the root.

Approach

Use a hash map from value to its inorder index. Walk preorder with a pointer pre_i. Recursively build subtrees from inorder ranges.

Why it works

Each root chosen from preorder partitions the inorder range into exact left and right subtree elements.

Code
Loading...
Complexity
Time: O(n)
Space: O(n)