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.
Be the first one to answer
Add answer anonymously...
Info Edge Senior Software Engineer 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