二叉树的后序遍历
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
AC代码
# 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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
def traival(root:TreeNode):
if not root:
return
else:
traival(root.left)
traival(root.right)
res.append(root.val)
res = []
traival(root)
return res
官方代码
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
def postorder(root: TreeNode):
if not root:
return
postorder(root.left)
postorder(root.right)
res.append(root.val)
res = list()
postorder(root)
return res
# 作者:LeetCode-Solution
现学现卖hhh