how can i build a tree from just preorder array ?
How to make a tree
Hey @Himanshu-Jhawar-2273952536067590
lets arr [ ] = {10,7,14,20,1,5,8}.
The first value in the array is 10, so the first step in constructing the tree will be to make 10 the root node
If the 7 is less than the 10, it will become the left child node. If the 7 is greater than or equal to 10 it will move to the right. Since we know that the 7 is less than 10 we designate it as the left child node,
the same pattern, we perform the same comparison with the 14 value in the array. Comparing the value of 14 to the root node of 10 we know that 14 is the right child.
Making our way through the array we come to the 20. We’ll start with comparing the array to 10, which it’s greater than. So we move to the right and compare it with 14, it’s greater than 14 and 14 doesn’t have any children to the right, so we make 20 the right child of 14.
Now we have the value 1. Following the same pattern as the other values we will compare 1 to 10, move to the left and compare it with 7, and finally make 1 the left child of the 7 node.
Will continue until we add all the elements in BST
