Asked inInfosys,SDE

Boundary Traversal of Binary Tree

Given a binary tree of integers, your task is to print the boundary nodes of the binary tree in an anti-clockwise direction starting from the root node.

Note:
The boundary includes nodes from the left boundary, leaf nodes, and the right boundary in sequence. Only unique nodes should be included in the output.

Input:

The input consists of multiple test cases. The first line contains an integer ‘T’ representing the number of test cases. Subsequent lines describe each test case in level-order form, with node values separated by spaces. Use -1 to denote null nodes.
Example:

For example, consider the binary tree input:
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Input for the tree is: 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1

Output:

For each test case, print the boundary nodes of the binary tree separated by single spaces. Output for each test case is on a separate line.
Constraints:
  • 1 ≤ T ≤ 102
  • 1 ≤ N ≤ 212
  • Time Limit: 1 sec
Note:
Implement the function to calculate the boundary traversal. The printing of the result is handled separately.
AnswerBot
15d

Boundary traversal of a binary tree in anti-clockwise direction starting from the root node.

  • Implement a function to calculate the boundary traversal of a binary tree

  • Include nodes from left boundary, l...read more

Help your peers!
Add answer anonymously...
Infosys SDE 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