Write an efficient algorithm to construct a full binary tree from a sequence of keys representing preorder traversal and a boolean array that determines if the value at the corresponding index in the preorder sequence is a leaf node or an internal node. A full binary tree is a tree in which every node has either 0 or 2 children.

For example,

Input:
 
Preorder traversal : { 1, 2, 4, 5, 3, 6, 8, 9, 7 }
Boolean array      : { 0, 0, 1, 1, 0, 0, 1, 1, 1 }
 
(1 represents a leaf node, and 0 represents an internal node)
 
 
Output: Below full binary tree

Full Binary Tree

Practice this problem

The idea is first to construct the full binary tree’s root node using the first key in the preorder sequence and then using the given boolean array, check if the root node is an internal node or a leaf node. If the root node is an internal node, recursively construct its left and right subtrees.

To construct the complete full binary tree, recursively repeat the above steps with subsequent keys in the preorder sequence. Following is the implementation of this approach in C++, Java, and Python:

C++


Download  Run Code

Output:

Preorder traversal of the constructed tree is 1 2 4 5 3 6 8 9 7

Java


Download  Run Code

Output:

Preorder traversal of the constructed tree is 1 2 4 5 3 6 8 9 7

Python


Download  Run Code

Output:

Preorder traversal of the constructed tree is 1 2 4 5 3 6 8 9 7

The time complexity of the above solution is O(n), where n is the total number of nodes in the binary tree. The auxiliary space required by the program is O(h) for the call stack, where h is the height of the tree.