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,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
Python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def __init__(self): self.temp_result = dict() self.result = list() self.level = 0 def levelOrderBottom(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ if root: self.return_node([root]) for i in range(0,len(self.temp_result)): self.result.append(self.temp_result[i]) return self.result[::-1] else: return [] def return_node(self, root_list): if root_list: temp_list = [] for i in root_list: temp_list.append(i.val) self.temp_result[self.level] = temp_list self.level += 1 new_root_list = list() for i in root_list: if i: if i.left: new_root_list.append(i.left) if i.right: new_root_list.append(i.right) print(new_root_list) return self.return_node(new_root_list) return