Check if each internal node of a BST has exactly one child | Check if each internal node of BST has only one child ; driver program to test above function
def hasOnlyOneChild ( pre , size ) : NEW_LINE INDENT nextDiff = 0 ; lastDiff = 0 NEW_LINE for i in range ( size - 1 ) : NEW_LINE INDENT nextDiff = pre [ i ] - pre [ i + 1 ] NEW_LINE lastDiff = pre [ i ] - pre [ size - 1 ] NEW_LINE if nextDiff * lastDiff < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT pre = [ 8 , 3 , 5 , 7 , 6 ] NEW_LINE size = len ( pre ) NEW_LINE if ( hasOnlyOneChild ( pre , size ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT