Assume we have a Node class with:
int valNode rightNode left
Input: binary tree of ints (you’ll be given root node)
Return the boundary traversal of the binary tree.
Example:
Input:
6
/ \
3 4
/ / \
5 1 0
\ /
2 8
/ \
9 7
Output:
[5, 9, 3, 2, 6, 1, 7, 4, 8, 0]
This problem asks for the boundary traversal of a binary tree: collect the left boundary excluding leaves, then all leaf nodes, and finally the right boundary excluding leaves in reverse order. The key is to avoid duplicating nodes that belong to multiple parts of the boundary, especially leaves and the root. A clean solution uses DFS/recursion with careful boundary checks in O(n) time.