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
- 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
- Diagonal Traversal of Binary Tree
- Density of Binary Tree in One Traversal
- Iterative diagonal traversal of binary tree
- If you are given two traversal sequences, can you construct the binary tree?
- Construct Special Binary Tree from given Inorder traversal
- Postorder traversal of Binary Tree without recursion and without stack
- Find n-th node in Postorder traversal of a Binary Tree
- Modify a binary tree to get preorder traversal using right pointers only
- Find n-th node in Preorder traversal of a Binary Tree
- Reverse Morris traversal using Threaded Binary Tree
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
Improved By : Ravi Teja Chowdary