Construct Binary Tree from Inorder and Postorder Traversal on LeetCode

🚀 Day 85 of #100DaysOfCode Solved 106. Construct Binary Tree from Inorder and Postorder Traversal on LeetCode 🔗 🧠 Key Insight: 👉 Postorder = Left → Right → Root 👉 Last element in postorder = root of tree 👉 Inorder = Left → Root → Right 👉 Use inorder to split into left & right subtrees ⚙️ Approach (HashMap + Recursion): 1️⃣ Take last element of postorder 🔹 This is the root 2️⃣ Find root index in inorder 🔹 Split inorder into:   • Left subtree   • Right subtree 3️⃣ Build subtrees recursively: 🔹 Important order: 👉 Build right subtree first, then left (because we are consuming postorder from end) 4️⃣ Use a HashMap: 🔹 Store inorder value → index 👉 For O(1) lookup ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DivideAndConquer #HashMap #Java #InterviewPrep #CodingJourney

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories