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] ]
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).
Binary Tree Level Order Traversal的扩展题~不过这题要求从底层往上层遍历~直接就在结果那儿reverse了一下,在网上找了找,好像也没看到有什么更好的解法~
class Solution:
# @param root, a tree node
# @return a list of lists of integers
def levelOrderBottom(self, root):
if root is None: return []
cur, res = [root], []
while cur:
next, tmp = [], []
for node in cur:
tmp.append(node.val)
if node.left:
next.append(node.left)
if node.right:
next.append(node.right)
res.append(tmp)
cur = next
return res[::-1]