问题描述 自底向上层序搜索
python代码
# 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: Optional[TreeNode]) -> List[List[int]]:
result = []
def BottomOrder(node,depth):
if node:
if depth==len(result):
result.insert(0,[])
result[-1-depth].append(node.val)
BottomOrder(node.left,depth+1)
BottomOrder(node.right,depth+1)
BottomOrder(root,0)
return result
运行结果: