Connect Nodes at Same Level Problem Statement

An assignment is to link adjacent nodes at the same level in a binary tree. Each node in a binary tree has at most two children (left and right) and a next pointer to the next right node at the same level. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are NULL.

Input:

The first line of input consists of an integer ‘T’, representing the number of test cases.
For each test case, the first line contains elements in level order format, separated by spaces. If a node is NULL, it is represented by -1.

Example tree input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1

Output:

For each test case, produce the level order as connected by next pointers, with '#' marking the end of each level.

Example output:
1 # 2 3 # 4 5 6 # 7 #

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
1 # 2 3 # 4 5 6 # 7 #

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 3000
  • -109 <= DATA <= 109

Ensure an efficient operation given N node constraints in each tree and constant space usage aside from input constraints.

Be the first one to answer
Add answer anonymously...
AlphaSense Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter