DevPath

← All problems

Maximum Value in a Tree

Easy trees

Description

Return the largest non-null node value.

Constraints

1 <= non-null nodes

Examples

Input: tree = [3,9,20,null,null,15,7]

Output: 20

Hints (1)
  • Take the maximum over non-null values.

Solutions

Max over non-null values. · Time O(n) · Space O(1)

func treeMax(tree []interface{}) int {
    max := -1 << 62
    for _, v := range tree {
        if n, ok := v.(float64); ok && int(n) > max {
            max = int(n)
        }
    }
    return max
}

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

Create account