题解 | #实现二叉树先序,中序和后序遍历#
实现二叉树先序,中序和后序遍历
https://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362
result_pre = [] result_mid = [] result_post = [] class Solution: def threeOrders(self , root: TreeNode) -> List[List[int]]: # write code here self.preOrder(root) self.midOrder(root) self.postOrder(root) result = [result_pre,result_mid,result_post] return result def preOrder(self, root: TreeNode): if not root: return result_pre.append(root.val) self.preOrder(root.left) self.preOrder(root.right) def midOrder(self, root: TreeNode): if not root: return self.midOrder(root.left) result_mid.append(root.val) self.midOrder(root.right) def postOrder(self, root: TreeNode): if not root: return self.postOrder(root.left) self.postOrder(root.right) result_post.append(root.val)