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).
思路同 二叉树层次遍历,最后结果反转下就可以了。
from typing import List
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
result = []
q = [root]
q_tmp = []
while q:
this_result = []
while q:
node = q.pop(0)
this_result.append(node.val)
if node.left:
q_tmp.append(node.left)
if node.right:
q_tmp.append(node.right)
result.insert(0, this_result)
q, q_tmp = q_tmp, q
return result