From 997ffb1061e6cb78a1004fd4a4718100fd750ba9 Mon Sep 17 00:00:00 2001 From: ORain <58782338+ORainn@users.noreply.github.com> Date: Mon, 29 Nov 2021 18:51:29 +0800 Subject: [PATCH] =?UTF-8?q?python=E9=80=92=E5=BD=92=E6=B3=95=20=E6=9B=B4?= =?UTF-8?q?=E5=BF=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python递归法 更快 --- problems/0654.最大二叉树.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index af945aca..361a92d1 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -256,6 +256,23 @@ class Solution { ## Python ```python +class Solution: + """递归法 更快""" + def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: + if not nums: + return None + maxvalue = max(nums) + index = nums.index(maxvalue) + + root = TreeNode(maxvalue) + + left = nums[:index] + right = nums[index + 1:] + + root.left = self.constructMaximumBinaryTree(left) + root.right = self.constructMaximumBinaryTree(right) + return root + class Solution: """最大二叉树 递归法"""