In hw3b, we used a for-loop. And that was the best solution for that problem. When we want to do something a definite number of times, 5 in this case, a for-loop is the best construct to use.
Answers
Answered by
0
Answer:
The first element of preorder traversal is always root. We first construct the root. Then we find the index of first element which is greater than root. Let the index be ‘i’. The values between root and ‘i’ will be part of left subtree, and the values between ‘i+1’ and ‘n-1’ will be part of right subtree. Divide given pre[] at index “i” and recur for left and right sub-trees.
For example in {10, 5, 1, 7, 40, 50}, 10 is the first element, so we make it root. Now we look for the first element greater than 10, we find 40
Similar questions