Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost.
Can we print all corner nodes using one traversal?
The idea is to use Level Order Traversal. Every time we store the size of the queue in a variable n, which is the number of nodes at that level. For every level we check whether the current node is the first (i.e node at index 0) and the node at last index (i.e node at index n-1) , If it is either of them , we print the value of that node .
15 10 20 8 25
Time Complexity : O(n) where n is number of nodes in Binary Tree.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print all nodes except rightmost node of every level of the Binary Tree
- Print all nodes in a binary tree having K leaves
- Print path between any two nodes in a Binary Tree | Set 2
- Print all full nodes in a Binary Tree
- Print the nodes having exactly one child in a Binary tree
- Print all even nodes of Binary Search Tree
- Print Nodes in Top View of Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Print nodes in top view of Binary Tree | Set 2
- Print the nodes of Binary Tree having a grandchild
- Print path between any two nodes in a Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print all internal nodes of a Binary tree
- Print all nodes between two given levels in Binary Tree
- Print all leaf nodes of a binary tree from right to left
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print nodes between two given level numbers of a binary tree
- Print all leaf nodes of a Binary Tree from left to right
- Print the nodes of binary tree as they become the leaf node