Print all nodes that don 't have sibling | A Binary Tree Node ; Function to print all non - root nodes that don 't have a sibling ; Base Case ; If this is an internal node , recur for left and right subtrees ; If left child is NULL , and right is not , print right child and recur for right child ; If right child is NULL and left is not , print left child and recur for left child ; Let us create binary tree given in the above example
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printSingles ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if root . left is not None and root . right is not None : NEW_LINE INDENT printSingles ( root . left ) NEW_LINE printSingles ( root . right ) NEW_LINE DEDENT elif root . right is not None : NEW_LINE INDENT print root . right . key , NEW_LINE printSingles ( root . right ) NEW_LINE DEDENT elif root . left is not None : NEW_LINE INDENT print root . left . key , NEW_LINE printSingles ( root . left ) NEW_LINE DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . right = Node ( 4 ) NEW_LINE root . right . left = Node ( 5 ) NEW_LINE root . right . left . left = Node ( 6 ) NEW_LINE printSingles ( root ) NEW_LINE