Boundary Traversal of a Binary Tree

Given a binary tree of integers, your task is to return the boundary nodes of the tree in Anti-Clockwise direction starting from the root node.

Input:

The first line contains an integer 'T' which denotes the number of test cases or queries.
Each test case consists of a single line containing elements in level order, with node values separated by spaces. Use -1 to denote a null node.

Output:

For each test case, return the boundary nodes of the given binary tree, separated by spaces.
Provide the output for each test case in a separate line.

Example:

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

Output:
1 2 4 7 6 3

Explanation:

  • The boundary nodes for the binary tree are: root node, left boundary, leaf nodes, and right boundary nodes.
  • If there are duplicates in node values from the boundaries, include them only once.

Constraints:

  • 1 <= T <= 10^2
  • 1 <= N <= 2^12 where 'N' is the total number of nodes in the binary tree.
  • Time Limit: 1 sec

Note:

The format of input is for clarity on forming a binary tree. Every tree is listed in a single line, separated by spaces.
Implement the function without printing as printing is already managed.
AnswerBot
4mo

Return the boundary nodes of a binary tree in Anti-Clockwise direction starting from the root node.

  • Traverse the left boundary nodes in top-down order

  • Traverse the leaf nodes in left-right order

  • Traverse...read more

Help your peers!
Select
Add answer anonymously...

Info Edge Senior Software Engineer interview questions & answers

A Senior Software Engineer was asked 2mo agoQ. Given two strings s and t, return the minimum window in s which will contain all...read more
A Senior Software Engineer was asked 12mo agoQ. Describe how you would build an MVC architecture.
A Senior Software Engineer was asked Q. Given a sorted array of integers nums and an integer target, write a function to...read more

Popular interview questions of Senior Software Engineer

A Senior Software Engineer was asked 2mo agoQ1. Given two strings s and t, return the minimum window in s which will contain all...read more
A Senior Software Engineer was asked 12mo agoQ2. Describe how you would build an MVC architecture.
A Senior Software Engineer was asked Q3. Given a sorted array of integers nums and an integer target, write a function to...read more
Info Edge Senior Software Engineer 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