Given a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root. For example, boundary traversal of the following tree is “20 8 4 10 14 25 22”
We break the problem in 3 parts:
1. Print the left boundary in top-down manner.
2. Print all leaf nodes from left to right, which can again be sub-divided into two sub-parts:
…..2.1 Print all leaf nodes of left sub-tree from left to right.
…..2.2 Print all leaf nodes of right subtree from left to right.
3. Print the right boundary in bottom-up manner.
We need to take care of one thing that nodes are not printed again. e.g. The left most node is also the leaf node of the tree.
Based on the above cases, below is the implementation:
20 8 4 10 14 25 22
Time Complexity: O(n) where n is the number of nodes in binary tree.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Iterative Boundary traversal of Complete Binary tree
- Sum of all the Boundary Nodes of a Binary Tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Density of Binary Tree in One Traversal
- Zig-Zag traversal of a Binary Tree using Recursion
- Diagonal Traversal of Binary Tree
- Kth node in Diagonal Traversal of Binary Tree
- Clockwise Spiral Traversal of Binary Tree
- If you are given two traversal sequences, can you construct the binary tree?
- Clockwise Spiral Traversal of Binary Tree | Set - 2
- Reverse zigzag Traversal of a Binary Tree
- Iterative diagonal traversal of binary tree
- Reverse Morris traversal using Threaded Binary Tree
- Density of Binary Tree using Level Order Traversal