Given a Binary Tree, print all nodes that don’t have a sibling (a sibling is a node that has same parent. In a Binary Tree, there can be at most one sibling). Root should not be printed as root cannot have a sibling.
For example, the output should be “4 5 6” for the following tree.
This is a typical tree traversal question. We start from root and check if the node has one child, if yes then print the only child of that node. If node has both children, then recur for both the children.
4 5 6
Time Complexity of above code is O(n) as the code does a simple tree traversal.
This article is compiled by Aman Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print levels with odd number of nodes and even number of nodes
- Left-Child Right-Sibling Representation of Tree
- Right sibling of each node in a tree given as array of edges
- Print all leaf nodes of an n-ary tree using DFS
- Print all neighbour nodes within distance K
- Print Leaf Nodes at a given Level
- Print the nodes at odd levels of a tree
- Print nodes at k distance from root
- Print all nodes at distance k from a given node
- Print all the levels with odd and even number of nodes in it | Set-2
- Print the nodes that are just above the leaf node
- Creating a tree with Left-Child Right-Sibling Representation
- Find right sibling of a binary tree with parent pointers
- Print all even nodes of Binary Search Tree
- Print all internal nodes of a Binary tree
- Print Nodes in Top View of Binary Tree
- Print all nodes that are at distance k from a leaf node
- Print the nodes of Binary Tree having a grandchild
- Print all full nodes in a Binary Tree
- Print all nodes in a binary tree having K leaves
Improved By : shrikanth13