Construct Tree from Preorder Traversal

Given a list of integers pre[] of size n, representing the preorder traversal of a special binary tree where each node has 0 or 2 children, and a boolean array isLeaf[] indicating if a node is a leaf. Write a function to construct the binary tree from the given arrays and return the root node.

Input:

The input consists of multiple test cases, each containing:

  • An integer t representing the number of test cases.
  • For each test case:
    • An integer n: the number of nodes in the binary tree.
    • A list of integers pre[] of length n, representing the preorder traversal.
    • A list isLeaf[], where 0 signifies a non-leaf node and 1 signifies a leaf node.

Output:

Return the root node of the constructed tree for each test case. Print the output in a new line for each test case.

Example:

Input:
t = 1
n = 7
pre = [1, 2, 4, 5, 3, 6, 7]
isLeaf = [0, 0, 1, 1, 0, 1, 1]
Output:
Root node of the constructed tree

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 10^3
  • -10^9 ≤ pre[i] ≤ 10^9
Note:
You are not required to print anything; just implement the function to construct the tree using the given arrays.
AnswerBot
4mo

Construct a binary tree from preorder traversal and leaf node information.

  • Create a binary tree using preorder traversal and leaf node information

  • Use recursion to build the tree

  • Handle both leaf and non...read more

Help your peers!
Select
Add answer anonymously...

Oracle Full Stack Developer interview questions & answers

A Full Stack Developer was asked Q. Implement a few design patterns.
A Full Stack Developer was asked Q. Bursting Balloons Problem Given an array ARR of size N, where each element repre...read more
A Full Stack Developer was asked Q. Root to Leaf Path Problem Statement Given a binary tree with 'N' nodes numbered ...read more

Popular interview questions of Full Stack Developer

A Full Stack Developer was asked Q1. Implement a few design patterns.
A Full Stack Developer was asked Q2. Bursting Balloons Problem Given an array ARR of size N, where each element repre...read more
A Full Stack Developer was asked Q3. Root to Leaf Path Problem Statement Given a binary tree with 'N' nodes numbered ...read more
Oracle Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits