def binary_tree_count_leaves_old(self, root): if not root: return 0 queue = Queue() queue.insert(root) leaf_count = 0 while queue.size()>0: node = queue.remove() if self.is_leaf(node): leaf_count +=1 if node.left: queue.insert(node.left) if node.right: queue.insert(node.right) return leaf_count
def height_of_tree(head): if head == None: return 0 queue = Queue() queue.insert(head) height = 0 while(1): children_count = queue.size() if children_count == 0: return height else: height += 1 while(children_count>0): node = queue.remove() if node.left: queue.insert(node.left) if node.right: queue.insert(node.right) children_count -= 1
def binary_tree_connect_nodes_at_same_level_to_linked_list(node): if node == None: return queue1 = Queue() queue1.insert(node) linked_list = None node_count = 1 temp_list = [] while not queue1.is_empty(): node_count = queue1.size() temp = queue1.remove() linked_list = insert_in_linked_list(linked_list, temp) node_count -= 1 if temp.left: temp_list.append(temp.left) if temp.right: temp_list.append(temp.right) if node_count == 0: print_linked_list(linked_list) for e in temp_list: queue1.insert(e) temp_list = [] linked_list = None