From b6807a66dc048c7098243a7c00c823eb3dceb3f1 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Wed, 28 Dec 2022 09:45:31 -0600 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0leetcode=20226=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91python=E5=90=8E=E5=BA=8F?= =?UTF-8?q?=E9=80=92=E5=BD=92=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index ad2a7de2..84c0c589 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -322,6 +322,18 @@ class Solution: return root ``` +递归法:后序遍历: +```python +class Solution: + def invertTree(self, root: TreeNode) -> TreeNode: + if root is None: + return None + self.invertTree(root.left) + self.invertTree(root.right) + root.left, root.right = root.right, root.left + return root +``` + 迭代法:深度优先遍历(前序遍历): ```python class Solution: