BETA

Difficulty Level: Medium

Submissions: 436 Accuracy:

43.12%

Construct Tree from Preorder Traversal (Function Problem)
       

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
          10
         /  \
        30   15
       /  \
      20   5


Input:
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.

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

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:

3
5
10 30 20 5 15 
N N L L L 
4
1 2 4 3 
N N L L 
6
1 2 4 6 5 3 
N N N L L L 
Output:
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


					   
It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result:

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