Problem
Problem page: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
Topics: Tree, Breadth-first Search
Difficulty: Easy
Similar problems: (E) Binary Tree Level Order Traversal
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree{3,9,20,#,#,15,7},
1
2
3
4
5 3
/ \
9 20
/ \
15 7return its bottom-up level order traversal as:
1
2
3
4
5 [
[15,7],
[9,20],
[3]
]confused what
"{1,#,2,3}"means?OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
2
3
4
5
6
7 1
/ \
2 3
/
4
\
5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}".
Analysis
I wonder if the idea was to use recursion to work up from the leaf nodes to the root node, adding each node to the corresponding level in the two-dimensional result array?
I just wanted to keep it simple, so I reverse the result array from Binary Tree Level Order Traversal...
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-binary-tree-level-order-traversal-ii/
