human
Write me a code to check if all levels of two trees are anagrams or not and the other steps required given the following:
Check if all levels of two trees are anagrams or not | Returns true if trees with root1 and root2 are level by level anagram , else returns false ..
Base Cases.
start level order traversal of two trees using two queues ..
n1 ( queue size ) indicates number of Nodes at current level in first tree and n2 indicates number of nodes in current level of second tree ..
If n1 and n2 are different.
If level order traversal is over.
Dequeue all Nodes of current level and Enqueue all Nodes of next level.
Check if nodes of current levels are anagrams or not ..
Utility function to create a new tree Node.
Driver Code.
Constructing both the trees ."
Solution in Python:
assistant
#Here is code to check if all levels of two trees are anagrams or not and the other steps required.
 def areAnagrams ( root1 , root2 ) :
	if ( root1 == None and root2 == None ) :
		return True
	if ( root1 == None or root2 == None ) :
		return False
	q1 = [ ]
	q2 = [ ]
	q1 . append ( root1 )
	q2 . append ( root2 )
	while ( 1 ) :
		n1 = len ( q1 )
		n2 = len ( q2 )
		if ( n1 != n2 ) :
			return False
		if ( n1 == 0 ) :
			break
		curr_level1 = [ ]
		curr_level2 = [ ]
		while ( n1 > 0 ) :
			node1 = q1 [ 0 ]
			q1 . pop ( 0 )
			if ( node1 . left != None ) :
				q1 . append ( node1 . left )
			if ( node1 . right != None ) :
				q1 . append ( node1 . right )
			n1 -= 1
			node2 = q2 [ 0 ]
			q2 . pop ( 0 )
			if ( node2 . left != None ) :
				q2 . append ( node2 . left )
			if ( node2 . right != None ) :
				q2 . append ( node2 . right )
			curr_level1 . append ( node1 . data )
			curr_level2 . append ( node2 . data )
		curr_level1 . sort ( )
		curr_level2 . sort ( )
		if ( curr_level1 != curr_level2 ) :
			return False
	return True
class newNode :
	def __init__ ( self , data ) :
		self . data = data
		self . left = self . right = None
if __name__ == ' _ _ main _ _ ' :
	root1 = newNode ( 1 )
	root1 . left = newNode ( 3 )
	root1 . right = newNode ( 2 )
	root1 . right . left = newNode ( 5 )
	root1 . right . right = newNode ( 4 )
	root2 = newNode ( 1 )
	root2 . left = newNode ( 2 )
	root2 . right = newNode ( 3 )
	root2 . left . left = newNode ( 4 )
	root2 . left . right = newNode ( 5 )
	if areAnagrams ( root1 , root2 ) :
		print ( " Yes " )
	else :
		print ( " No " )