Find n | Python3 program for nth node of inorder traversal ; A Binary Tree Node ; Given a binary tree , print the nth node of inorder traversal ; first recur on left child ; when count = n then prelement ; now recur on right child ; Driver Code
count = [ 0 ] NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE self . visited = False NEW_LINE DEDENT DEDENT def NthInorder ( node , n ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( count [ 0 ] <= n ) : NEW_LINE INDENT NthInorder ( node . left , n ) NEW_LINE count [ 0 ] += 1 NEW_LINE if ( count [ 0 ] == n ) : NEW_LINE INDENT print ( node . data , end = " ▁ " ) NEW_LINE DEDENT NthInorder ( node . right , n ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 20 ) NEW_LINE root . right = newNode ( 30 ) NEW_LINE root . left . left = newNode ( 40 ) NEW_LINE root . left . right = newNode ( 50 ) NEW_LINE n = 4 NEW_LINE NthInorder ( root , n ) NEW_LINE DEDENT