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
4mo
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 & answers
A SDE was asked 10mo agoQ. What is annotation in Spring Boot?
A SDE was asked Q. Count Inversions Problem Statement Given an integer array ARR of size N containi...read more
A SDE was asked Q. Boundary Traversal of Binary Tree Given a binary tree of integers, your task is ...read more
Popular interview questions of SDE
A SDE was asked 10mo agoQ1. What is annotation in Spring Boot?
A SDE was asked Q2. Count Inversions Problem Statement Given an integer array ARR of size N containi...read more
A SDE was asked Q3. Boundary Traversal of Binary Tree Given a binary tree of integers, your task is ...read more
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

