題目
原題頁面: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/zh-tw/leetcode-binary-tree-level-order-traversal-ii/
