From 13e7e637a77273921e6b15475dd899d39e3c84dd Mon Sep 17 00:00:00 2001 From: charon2121 <66763177+charon2121@users.noreply.github.com> Date: Mon, 17 May 2021 23:50:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20144.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=89=8D=E5=BA=8F=E9=81=8D=E5=8E=86python3?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 94.二叉树的中序遍历python3版本 添加 145.二叉树的后序遍历python3版本 --- problems/二叉树的递归遍历.md | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index e8cf0bdd..937ef603 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -170,7 +170,53 @@ class Solution { ``` Python: +```python3 +# 前序遍历-递归-LC144_二叉树的前序遍历 +class Solution: + def preorderTraversal(self, root: TreeNode) -> List[int]: + # 保存结果 + result = [] + + def traversal(root: TreeNode): + if root == None: + return + result.append(root.val) # 前序 + traversal(root.left) # 左 + traversal(root.right) # 右 + traversal(root) + return result + +# 中序遍历-递归-LC94_二叉树的中序遍历 +class Solution: + def inorderTraversal(self, root: TreeNode) -> List[int]: + result = [] + + def traversal(root: TreeNode): + if root == None: + return + traversal(root.left) # 左 + result.append(root.val) # 中序 + traversal(root.right) # 右 + + traversal(root) + return result + +# 后序遍历-递归-LC145_二叉树的后序遍历 +class Solution: + def postorderTraversal(self, root: TreeNode) -> List[int]: + result = [] + + def traversal(root: TreeNode): + if root == None: + return + traversal(root.left) # 左 + traversal(root.right) # 右 + result.append(root.val) # 后序 + + traversal(root) + return result +``` Go: