#C4724. Bottom-Up Level Order Traversal of a Binary Tree

    ID: 48294 Type: Default 1000ms 256MiB

Bottom-Up Level Order Traversal of a Binary Tree

Bottom-Up Level Order Traversal of a Binary Tree

You are given a binary tree represented in level order format where null indicates a missing node. Your task is to perform a bottom‐up level order traversal of the tree, i.e. traverse the tree level by level from bottom to top.

The binary tree is defined by the recursive formula: \( T = (v, left, right) \), where v is the node value, and left and right are its children (which can be null).

For example, given the binary tree represented as:

[3, 9, 20, null, null, 15, 7]

the bottom-up level order traversal is:

[[15, 7], [9, 20], [3]]

Read the input from standard input and output the result to standard output.

inputFormat

The input is a single line containing a list representation of the binary tree in level order. The format is:

[a1, a2, a3, ..., an]

where each ai is either an integer or the string null (without quotes) indicating a missing node.

Examples:

  • [3, 9, 20, null, null, 15, 7]
  • [1, 2, 3, 4, 5]

outputFormat

Output the bottom-up level order traversal of the tree. The result should be a list of lists, where each inner list contains the node values at that level. The levels are printed from bottom to top.

Examples:

  • For input [3, 9, 20, null, null, 15, 7], the output should be: [[15, 7], [9, 20], [3]]
  • For input [1, 2, 3, 4, 5], the output should be: [[4, 5], [2, 3], [1]]
## sample
[3, 9, 20, null, null, 15, 7]
[[15, 7], [9, 20], [3]]