You are given a binary tree. Count the total number of nodes it contains.
One line: the binary tree as a level-order array.
The tree is encoded on ONE line as a space-separated level-order (breadth-first) array. The token null marks a missing child; the children of a null are omitted from the array. An empty tree is written as the single token null.
A single integer: the number of nodes in the tree (0 for an empty tree).
Example 1
Input
1 2 3 4 5 null 6
Expected
6
Explanation
The tree holds the values 1, 2, 3, 4, 5 and 6, so there are 6 nodes.
Example 2
Input
42
Expected
1
Explanation
A tree that is just a single root node has 1 node.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →