Boundary Traversal

Medium Problems DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Boundary Traversal of a Binary Tree, Binary Tree, Traversal, Tree Traversal, Recursion, Algorithm, Time Complexity, Space Complexity, Node, Data Structures, boundary tracking, space complexity, tree properties, tree traversal, binary tree, preorder traversal, recursion, time complexity analysis, traversal, Boundary Traversal.

Boundary Traversal of a Binary Tree Problem Statement Given the root of a binary tree, return the values of its boundary nodes in an anti clockwise direction. The boundary traversal includes the root node, all nodes on the left boundary (excluding leaf nodes), all leaf nodes (traversed left to right), and all nodes on the right boundary (excluding leaf nodes), traversed in reverse order from bottom to top. Input Specification The input consists of a single line representing the binary tree in level order traversal. Null nodes are represented by 1. For example, 1 2 3 1 1 4 5 represents a tree where 1 is the root, 2 is its left child, 3 is its right child, 4 is 3's left child, and 5 is 3's right child. Each node's value will be an integer. Output Specification Return a list of integers representing the values of the boundary nodes in anti clockwise order. Sample Test Cases Example 1: Input: 1 2 7 3 1 1 8 1 4 9 1 5 6 10 11 Output: [1, 2, 3, 4, 5, 6, 10, 11, 9, 8, 7] Explanation: The boundary traversal visits: the root (1), left boundary (2, 3, 4), leaf nodes (5, 6, 10, 11), and right boundary in reverse (9, 8, 7). Example 2: Input: 10 5 20 3 8 18 25 1 7 1 1 Output: [10, 5, 3, 7, 8, 18, 25, 20] Explanation: The boundary traversal visits: the root (10), left boundary (5), leaf nodes (3, 7, 8, 18, 25), and right boundary in reverse (20).