EasyBinary SearchBit ManipulationTreeBinary Tree

1 approach, code in every language

Problem description

You are given the root of a complete binary tree. In a complete tree every level is fully filled except possibly the last, and the last level is packed as far left as possible.

Return the total number of nodes. A plain traversal is O(n); the goal here is to exploit the completeness property to do better.

Example 1

Input: root = [1,2,3,4,5,6]

Output: 6

The tree has two full levels plus two extra nodes on the last level.

Example 2

Input: root = [1]

Output: 1

A single root node counts as one.

Constraints

  • The number of nodes in the tree is in the range [0, 5 * 10^4].
  • 0 <= Node.val <= 5 * 10^4
  • The tree is guaranteed to be complete.

After the pattern

Now solve one you haven't seen.

Counting the Chute Junctions is our own problem on the technique behind Count Complete Tree Nodes: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free