Boundary Traversal Problem Statement

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

Example:

Input:
T = 1
Data for binary tree (in level order):
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 are the nodes on the left boundary, followed by leaves, followed by right boundary nodes without repeating any nodes.

Constraints:

  • 1 ≤ T ≤ 102
  • 1 ≤ N ≤ 212
  • 'N' is the total number of nodes in the binary tree.
  • Time Limit: 1 sec
Note:

The input format was provided for clarity. Each test case should be processed with the entire node sequence given in a single line, separated by spaces where -1 denotes null nodes.

AnswerBot
4d

Return the boundary nodes of a binary tree in an anti-clockwise direction starting from the root node.

  • Traverse the left boundary nodes in a top-down manner

  • Traverse the leaf nodes from left to right

  • Tra...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer 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