
Asked in Morgan Stanley
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!
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
Interview Questions Asked to Digital Technology Intern at Other Companies
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

