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
Be the first one to answer
Add answer anonymously...
Top Intuit SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app