Home › Company-wise questions › Deloitte › Construct Binary Tree from Preorder and Inorder ← Deloitte bank
Deloitte · Question 65 · Trees
Construct Binary Tree from Preorder and Inorder Trees Java code ⏱ O(n) 💾 O(n)
Service-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Rebuild a tree using preorder root order and inorder left/right partition.
Approach Use a hashmap for inorder indexes.
Time O(n)Space O(n)
How it works 1 Preorder head is the subtree root. 2 Its inorder index splits left/right ranges. Sample input / output Input:
preorder = [3, 9, 20, 15, 7], inorder = [9, 3, 15, 20, 7]
Output:
[3, 9, 20, 15, 7] Constraints Unique values; valid traversals.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n)Space O(n)Loops 3Branches 7Returns 3source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE class TreeNode DEFINE val TreeNode left, right TreeNode(int v) { val = v; } DEFINE TreeNode buildTree(Integer[] values) if (values.length == 0 || values[0] == null) return null TreeNode root = new TreeNode(values[0]) java.util.Queue<TreeNode> q = new java.util.LinkedList<>() q.offer(root) DEFINE i = 1 while (!q.isEmpty() && i < values.length) TreeNode node = q.poll() if (i < values.length && values[i] != null) { node.left = ne… i++ if (i < values.length && values[i] != null) { node.right = ne… i++ RETURN root DEFINE String levelOrder(TreeNode root) if (root == null) return "[]" StringBuilder sb = new StringBuilder("[") java.util.Queue<TreeNode> q = new java.util.LinkedList<>() q.offer(root) DEFINE first = true while (!q.isEmpty()) TreeNode node = q.poll() if (node == null) continue if (!first) sb.append(", ") first = false sb.append(node.val) q.offer(node.left) q.offer(node.right) RETURN sb.append("]").toString() DEFINE Map<Integer, Integer> idx = new HashMap<>() DEFINE int preIdx DEFINE static TreeNode buildTree(int[] preorder, int[] inorder) preIdx = 0 idx.clear() for (int i = 0; i < inorder.length; i++) idx.put(inorder[i], i) RETURN helper(preorder, 0, inorder.length - 1) DEFINE static TreeNode helper(int[] pre, int l, int r) if (l > r) return null DEFINE val = pre[preIdx++] TreeNode root = new TreeNode(val) start / end statement condition loop returns definition← Q64 · Path Sum II Q66 · Construct Binary Tree from Inorder and Postorder → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →