DevPath

← All problems

Count Tree Nodes

Easy trees

Description

Return the number of non-null nodes in the heap-style tree.

Constraints

0 <= n <= 100000

Examples

Input: tree = [1,2,3]

Output: 3

Hints (1)
  • Count the non-null entries.

Solutions

Count non-null entries. · Time O(n) · Space O(1)

func treeCountNodes(tree []interface{}) int {
    count := 0
    for _, v := range tree {
        if v != nil {
            count++
        }
    }
    return count
}

Create a free account to submit solutions, track progress and save problems.

Create account