To print the boundary of Binary Tree
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In binary tree structures, each node has at most two children. Binary trees are foundational to numerous algorithms and data structures, making their understanding crucial for computer science. One interesting aspect of binary trees is their boundary traversal, which is a way to represent the tree's outermost edges. Boundary traversal involves printing the nodes from the leftmost path, followed by leaf nodes, and finally the rightmost path. This comprehensive traversal ensures that the tree's perimeter is represented.
Boundary Traversal Strategy
The boundary of a binary tree is printed using the combination of the following components:
- Left Boundary Nodes: These are nodes found on the leftmost path, excluding leaf nodes.
- Leaf Nodes: These nodes are found on the bottommost level of the tree and do not have left or right children.
- Right Boundary Nodes: These nodes are found on the rightmost path, excluding leaf nodes, traversed in such a way that they form a reverse order.
Steps for Boundary Traversal
- Print the root node if it exists. This step initiates the process of boundary traversal since it is considered the top boundary node.
- Traverse the left boundary, beginning with the root's left child, and print nodes until a leaf node is encountered.
- Print all leaf nodes from left to right.
- Traverse the right boundary, beginning with the root's right child, and print nodes until a leaf node is encountered. This traversal should be added to the final output in reverse order to maintain the porous structure of the tree's boundary.
Technical Explanation
Let's explore a Python function that performs boundary traversal on a binary tree.
- Single Node Tree: If the tree only contains the root node, this node should be output as the only boundary.
- Left-Skewed Tree: The algorithm will print nodes from top to bottom and then bottom to top without duplication.
- Right-Skewed Tree: Similarly, it adheres to the strict boundary with distinct node printing.
Related reading
- Toilet Seat Algorithm
- Topological sort based on a comparator rather than a graph
- Topological sort, but with a certain kind of grouping
- Topological sort in OCaml
- .toArraynew MyClass0 or .toArraynew MyClassmyList.size?
- ''too many values to unpack'', iterating over a dict. keystring, valuelist
- Topological sort of cyclic graph with minimum number of violated edges
- Topological sort to find the number of paths to t

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.