From 8a7e5ca6f7b5b198efebf7fe82749a3a3fd28df2 Mon Sep 17 00:00:00 2001 From: laozhang <1044985343@qq.com> Date: Sat, 16 May 2020 19:57:03 +0800 Subject: [PATCH] python leetcode 226 and 700 --- python-leetcode/README.md | 4 +++- .../solution/tree/leetcode_226_.py | 17 +++++++++++++++ python-leetcode/solution/tree/leetcode_27_.py | 6 ------ python-leetcode/solution/tree/leetcode_55_.py | 2 +- .../solution/tree/leetcode_700_.py | 21 +++++++++++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 python-leetcode/solution/tree/leetcode_226_.py create mode 100644 python-leetcode/solution/tree/leetcode_700_.py diff --git a/python-leetcode/README.md b/python-leetcode/README.md index 180b7d6b..e171ce23 100644 --- a/python-leetcode/README.md +++ b/python-leetcode/README.md @@ -8,4 +8,6 @@ ## 二叉树专题 1. [二叉树的镜像](./solution/tree/leetcode_27_.py) -1. [二叉树的深度](./solution/tree/leetcode_55_.py) +2. [二叉树的深度](./solution/tree/leetcode_55_.py) +2. [二叉树的反转](./solution/tree/leetcode_226_.py) +2. [二叉搜索树中的搜索](./solution/tree/leetcode_700_.py) diff --git a/python-leetcode/solution/tree/leetcode_226_.py b/python-leetcode/solution/tree/leetcode_226_.py new file mode 100644 index 00000000..c433a5c2 --- /dev/null +++ b/python-leetcode/solution/tree/leetcode_226_.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# coding=utf-8 +""" +226. 反转二叉树 +""" +from solution import TreeNode + + +class Solution: + def invertTree(self, root: TreeNode) -> TreeNode: + if root is None: + return None + temp = root.right + root.right = self.invertTree(root.left) + root.left = self.invertTree(temp) + return root diff --git a/python-leetcode/solution/tree/leetcode_27_.py b/python-leetcode/solution/tree/leetcode_27_.py index ca644834..7476c0f3 100644 --- a/python-leetcode/solution/tree/leetcode_27_.py +++ b/python-leetcode/solution/tree/leetcode_27_.py @@ -3,12 +3,6 @@ # coding=utf-8 """ 27. 二叉树的镜像 - -所以其他Python代码参考本例格式,通用数据结构放在init中,禁止使用Python2 - -可增加main方法和测速case 或 ac结果 - -提交前使用IDEA的format code格式化代码 """ from solution import TreeNode diff --git a/python-leetcode/solution/tree/leetcode_55_.py b/python-leetcode/solution/tree/leetcode_55_.py index 1e5385ec..8f094902 100644 --- a/python-leetcode/solution/tree/leetcode_55_.py +++ b/python-leetcode/solution/tree/leetcode_55_.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # coding=utf-8 """ -27. 二叉树的镜像 +55. 二叉树的深度 """ from solution import TreeNode diff --git a/python-leetcode/solution/tree/leetcode_700_.py b/python-leetcode/solution/tree/leetcode_700_.py new file mode 100644 index 00000000..1bd11c43 --- /dev/null +++ b/python-leetcode/solution/tree/leetcode_700_.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# coding=utf-8 +""" +700. 二叉树中的搜索 +""" +from solution import TreeNode + + +class Solution: + res = None + + def searchBST(self, root: TreeNode, val: int) -> TreeNode: + if root is None: + return + if root.val == val: + self.res = root + return self.res + self.searchBST(root.left, val) + self.searchBST(root.right, val) + return self.res -- GitLab