#C2638. Maximum Level Sum in a Binary Tree

    ID: 45976 Type: Default 1000ms 256MiB

Maximum Level Sum in a Binary Tree

Maximum Level Sum in a Binary Tree

You are given a binary tree where each node contains an integer value. Your task is to find the maximum sum of the values at any level of the tree. The level of the root node is 0, its children are level 1, and so on.

Input Format: The binary tree is given in level order as a space-separated list of tokens. The token null is used to denote a missing node. For example, a tree with a single node will be provided as 1 and an empty tree as null.

Task: Compute the maximum level sum over all levels in the binary tree. If the tree is empty, output 0.

Note: If there are negative values, the level with the highest sum might be the one having only a single positive node.

inputFormat

The input is provided via standard input (stdin) as a single line containing space-separated values, representing the level order traversal of the binary tree. Use the token null (without quotes) to represent missing nodes.

Examples:

  • 1 represents a tree with a single node with value 1.
  • 1 2 3 represents a tree with root node value 1, left child 2, and right child 3.

outputFormat

Output a single integer to standard output (stdout) which is the maximum sum of values among all levels in the binary tree.

## sample
1
1