题目
原题页面:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
题目类型:Tree, Breadth-first Search
难度评价:Easy
类似题目:(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}".
分析
不知道出题意图是不是希望用递归,从叶子节点到根结点,将每一个节点加入到结果二维数组的相应层次中?
不过我这里就追求简单,直接将Binary Tree Level Order Traversal的结果数组reverse一下。。。
代码
1 | class Solution: |
本文地址:https://xnerv.wang/leetcode-binary-tree-level-order-traversal-ii/
