Back to Browse

105. Construct Binary Tree from Preorder and Inorder Traversal | DSA | Logic, and Code Explained

25 views
Oct 31, 2022
22:09

#java #dsa #coding #programming #datastructures #leetcode Problem Statement: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Algorithm: 1. Pick an element from Preorder. Increment a Preorder Index Variable (preIndex) to pick the next element in the next recursive call. 2. Create a new Treenode root with the data as the picked element. 3. Find the picked element’s index in Inorder array. Let the index be rootValIndex. 4. Call buildTree for elements before rootValIndex-1 and make the built tree as a left subtree of root. 5. Call buildTree for elements after rootValIndex+1 and make the built tree as a right subtree of root. 6. return root.

Download

0 formats

No download links available.

105. Construct Binary Tree from Preorder and Inorder Traversal | DSA | Logic, and Code Explained | NatokHD