Check if all the leaves are at the same level in a binary tree
Anonymous
def leaf_level(root,level,firstlevel): if root is None: return True if root.left is None and root.right is None: #leaf encountered if(firstlevel == 0): #first leaf firstlevel = level return True elif level == firstlevel: #check if leaf is at same level as first leaf return True else: return False #recursively call on its children return leaf_level(root.left,level+1,firstlevel) and leaf_level(root.right,level+1,firstlevel) leaf_level(Tree.root,0,0)
Check out your Company Bowl for anonymous work chats.