Solved LeetCode #105: Construct Binary Tree from Preorder and Inorder

🚀 Just solved LeetCode #105 – Construct Binary Tree from Preorder and Inorder Traversal 🌳✅ 🧩 Problem: Given two integer arrays, preorder and inorder, where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, the goal is to construct and return the binary tree. 💡 My Approach: I used recursion to rebuild the tree based on the properties of preorder and inorder traversals. 1️⃣ The first element of the preorder array is always the root of the tree. 2️⃣ Find that root in the inorder array — elements to the left form the left subtree, and elements to the right form the right subtree. 3️⃣ Recursively build the left and right subtrees using the corresponding segments of preorder and inorder arrays. 4️⃣ Return the constructed root node once the recursion completes. This approach efficiently utilizes the traversal properties to reconstruct the binary tree from scratch. 🌲 📘 What I Learned: 🌱 Strengthened my understanding of how preorder and inorder traversals relate to each other. 🧠 Gained deeper insight into recursive problem-solving and subarray management. 🔁 Improved my ability to think recursively and break down problems into smaller components. 💻 Appreciated how recursion beautifully models hierarchical data structures like trees. #LeetCode #Java #ProblemSolving #CodingJourney #DataStructures #BinaryTree

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories