root of a binary tree that is packed: every level except possibly the last is completely full, and the nodes of the last level sit side by side starting from the leftmost position.
Return the number of nodes in the tree.
Visiting every node takes O(n). Use the shape of the tree to do better.root=tree_node([4, 9, 2, 7, 1]) → 5
The top two levels are full (3 nodes), and the bottom level has two nodes packed to the left: 5 in total.
root=tree_node([]) → 0
An empty tree has zero nodes.
root=tree_node([6, 3, 8, 0, 5, 2, 9]) → 7
All three levels are full: 1 + 2 + 4 = 7.
Node.val ≤ 5·10⁴root=tree_node([4, 9, 2, 7, 1])
5
Run checks the first 3 tests in your browser. Submit checks all 49 tests on the server, hidden ones included.