Profile a Binary Tree
Walk a binary tree stored in a list level by level and report its node count, height and number of leaves.
The problem
Implement binary_tree_profile(level_order). The list stores a binary tree by position: the root is at index 0, and the children of the node at index i are at indexes 2 * i + 1 and 2 * i + 2. None means there is no node at that position. A value belongs to the tree only if you can reach it from the root, so values below a missing node are ignored. In [1, None, 3, 4, 5], the values 4 and 5 sit under the missing node at index 1, so the tree has just the nodes 1 and 3. Return [node_count, height, leaf_count]. height is the number of nodes on the longest path from the root down, so a single node has height 1. A leaf is a node with no children. An empty list or a None root means there is no tree: return [0, 0, 0]. Example: [1, 2, 3, 4, None, None, 7] returns [5, 3, 2]: five nodes, three levels, and two leaves (4 and 7).
Starter code
def binary_tree_profile(level_order):
passPublic examples
level_order = [1, 2, 3, 4, None, None, 7]gives[5, 3, 2]level_order = []gives[0, 0, 0]
Solving it in the practice book needs an active paid plan.
Open it in the practice book