Continue learning
Practice related DSA questions in the same topic.
Return the maximum number of nodes along a root-to-leaf path in a complete binary tree encoded in level order.
Return the maximum number of nodes along a root-to-leaf path in a complete binary tree encoded in level order.
def solve(values, target): node = 0 depth = 0 while (node < len(values)): depth = (depth + 1) node = ((node * 2) + 1) return depthCount the level at node 0.
Count the level at node 0.
On a full row of seats filled left to right, the left aisle reaches every occupied row.
In a complete tree the leftmost path reaches the deepest occupied level.
O(log(n + 1)) time · O(1) auxiliary space
Worked example · {"values":[8,4,9,1,6],"target":0} ⇒ 3
Edge case 1 · {"values":[],"target":0} ⇒ 0
Edge case 2 · {"values":[7],"target":0} ⇒ 1
Following only left links would be incorrect for a general sparse tree.
Which traversal would compute depth in a sparse tree?