Asked inIntuit,SDE-2

Boundary Traversal of Binary Tree Problem Statement

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

Note:

The boundary nodes comprise the left boundary, right boundary, and all the leaf nodes, without including any duplicates. Even if the node values are duplicates, include them only once.

Input:

'T' which denotes the number of test cases followed by binary tree nodes in level order. Use '-1' to represent null nodes. The input is a single line of node values separated by spaces.

Output:

Print the boundary nodes for each test case, separated by spaces, each test case on a new line.

Example:

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

The binary tree level order input describes the tree as follows -

Level 1: Root node is 1
Level 2: Left child of 1 is 2, Right child is 3
Level 3: Left child of 2 is 4, Right child is null
......
Boundary order: 1 -> 2 -> 4 -> 7 (leaf nodes) -> 5 -> 6 -> 3

Constraints:

  • 1 ≤ T ≤ 102
  • 1 ≤ N ≤ 212 (where 'N' is the total number of nodes)
  • Time Limit: 1 sec
AnswerBot
4mo

Boundary traversal of a binary tree to find left boundary, right boundary, and leaf nodes in an anti-clockwise direction.

  • Perform a pre-order traversal to get the left boundary nodes

  • Perform an in-order...read more

Help your peers!
Select
Add answer anonymously...

Intuit SDE-2 interview questions & answers

A SDE-2 was asked Q. Boundary Traversal of Binary Tree Problem Statement Given a binary tree consisti...read more
A SDE-2 was asked Q. Balanced Parentheses Combinations Given an integer N representing the number of ...read more
A SDE-2 was asked Q. How would you design a system like Pastebin?

Popular interview questions of SDE-2

A SDE-2 was asked Q1. Boundary Traversal of Binary Tree Problem Statement Given a binary tree consisti...read more
A SDE-2 was asked Q2. Balanced Parentheses Combinations Given an integer N representing the number of ...read more
A SDE-2 was asked Q3. How would you design a system like Pastebin?
Intuit SDE-2 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