题解 | #实现二叉树先序,中序和后序遍历#
实现二叉树先序,中序和后序遍历
https://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 the root of binary tree # @return int整型二维数组 # class Solution: def __init__(self): self.ls_pre=[] self.ls_in=[] self.ls_post=[] def pre_order(self,node): if node: self.ls_pre.append(node.val) self.pre_order(node.left) self.pre_order(node.right) def in_order(self,node): if node: self.in_order(node.left) self.ls_in.append(node.val) self.in_order(node.right) def post_order(self,node): if node: self.post_order(node.left) self.post_order(node.right) self.ls_post.append(node.val) def threeOrders(self , root: TreeNode) -> List[List[int]]: res=[] self.pre_order(root) res.append(self.ls_pre) self.in_order(root) res.append(self.ls_in) self.post_order(root) res.append(self.ls_post) return res