107. Binary Tree Level Order Traversal II
Posted panini
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了107. Binary Tree Level Order Traversal II相关的知识,希望对你有一定的参考价值。
题目:
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}
,
3 / 9 20 / 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
链接: http://leetcode.com/problems/binary-tree-level-order-traversal-ii/
一刷,比102题只多了最后一步,还是用了2个list,第二遍可以试一下DFS
1 class Solution(object): 2 def levelOrderBottom(self, root): 3 if not root: 4 return [] 5 result = [] 6 prev = [root] 7 cur = [] 8 9 while prev: 10 for elem in prev: 11 if elem.left: 12 cur.append(elem.left) 13 if elem.right: 14 cur.append(elem.right) 15 result.append([e.val for e in prev]) 16 prev, cur = cur, [] 17 result.reverse() 18 return result
以上是关于107. Binary Tree Level Order Traversal II的主要内容,如果未能解决你的问题,请参考以下文章
107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II
LeetCode107. Binary Tree Level Order Traversal II 解题报告
[LeetCode]题解(python):107 Binary Tree Level Order Traversal II