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 from root to leaf nodes in a top-down manner

  • Traverse the leaf nodes ...read more

Help your peers!
Select
Add answer anonymously...

Top Digital Technology Intern Interview Questions Asked at Morgan Stanley

Q. Minimum Number of Platforms Problem Your task is to determine the minimum number...read more
Q. Next Greater Number Problem Statement Given a string S which represents a number...read more
Q. Find the Second Largest Element Given an array or list of integers 'ARR', identi...read more
Digital Technology Intern 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