Tap here to switch tabs
Problem
Submissions

Problem: Zigzag Level Sum of Binary Tree

Zigzag Level Sum of Binary Tree returns one sum per level, scanning odd levels left to right and even levels right to left, adding each required child's value and stopping as soon as that child is missing.

Statement

Given the root of a binary tree, compute and return a list of integers representing the zigzag level sum for each level of the tree.

The tree levels are 11 indexed, where the root is at level 11. For each level:

  • If the level number is odd, iterate through the nodes from left to right. For each node, if it has a left child, add its value to the level sum. If a node does not have a left child, stop processing that level immediately.

  • If the level number is even, iterate through the nodes from right to left. For each node, if it has a right child, add its value to the level sum. If a node does not have a right child, stop processing that level immediately.

Return a list containing the resulting sum for each level from level 11 to the maximum depth of the tree.

Constraints:

  • The number of nodes in the tree is in the range [1,105][1, 10^5].

  • −105≤-10^5 \leq Node.val ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Zigzag Level Sum of Binary Tree

Zigzag Level Sum of Binary Tree returns one sum per level, scanning odd levels left to right and even levels right to left, adding each required child's value and stopping as soon as that child is missing.

Statement

Given the root of a binary tree, compute and return a list of integers representing the zigzag level sum for each level of the tree.

The tree levels are 11 indexed, where the root is at level 11. For each level:

  • If the level number is odd, iterate through the nodes from left to right. For each node, if it has a left child, add its value to the level sum. If a node does not have a left child, stop processing that level immediately.

  • If the level number is even, iterate through the nodes from right to left. For each node, if it has a right child, add its value to the level sum. If a node does not have a right child, stop processing that level immediately.

Return a list containing the resulting sum for each level from level 11 to the maximum depth of the tree.

Constraints:

  • The number of nodes in the tree is in the range [1,105][1, 10^5].

  • −105≤-10^5 \leq Node.val ≤105\leq 10^5