Construct Tree from Preorder Traversal
Submissions: 1040   Accuracy:


  Difficulty: Medium

Given an array ‘pre[]’ that represents Preorder traversal of a binary tree where every node has either 0 or 2 children. One more array ‘preLN[]’ is given which has only two possible values ‘L’ and ‘N’. The value ‘L’ in ‘preLN[]’ indicates that the corresponding node in Binary Tree is a leaf node and value ‘N’ indicates that the corresponding node is non-leaf node.

Your task is to complete the function constructTree(), that constructs the tree from the given two arrays and return the root pointer to new binary tree formed.
For Example:

Input:  pre[] = {10, 30, 20, 5, 15},  preLN[] = {'N', 'N', 'L', 'L', 'L'}
Output: Root of following tree
         /  \
        30   15
       /  \
      20   5

The function takes three arguments as input, first an integer N, denoting the size of both the array, second an array "pre[]"  that represents Preorder traversal of the binary tree and the last argument an character array "preLN[]" which indicates that the corresponding node in Binary Tree is a leaf node or a normal node.
There will be T, test cases and for each test case the function will be called separately.

For each test case return the reference pointer to the root node of the new binary tree formed.



10 30 20 5 15 
N N L L L 
1 2 4 3 
N N L L 
1 2 4 6 5 3 
N N N L L L 
20 30 5 10 15 
4 2 3 1 
6 4 5 2 3 1 

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: harshitsidhwa

Need help with your code? Please use, generate link and share the link here.