#C14226. Binary Tree Level Order Traversal II

    ID: 43852 Type: Default 1000ms 256MiB

Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II

You are given a binary tree, and your task is to return the bottom-up level order traversal of its nodes' values. This means you should traverse the tree level by level from top to bottom, but output the result starting from the lowest level up to the root.

The binary tree is provided as a single line input in level order where each node is separated by commas. Use the keyword null to represent a missing node.

For example, given the input 3,9,20,null,null,15,7, the tree is structured as follows:

[ 3 \ / \ 9 20 \ / \ 15 7 ]

The bottom-up level order traversal is:

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

inputFormat

The input is given as a single line from stdin representing the level order traversal of a binary tree. Each value is separated by a comma. Use the string null to denote a missing node.

Examples of valid inputs:

  • null (represents an empty tree)
  • 1 (a single node tree)
  • 3,9,20,null,null,15,7

outputFormat

Output the bottom-up level order traversal as a list of lists. The output should be written to stdout following the JSON array format.

For example: [[15,7],[9,20],[3]]

## sample
null
[]