File tree Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -256,7 +256,25 @@ class Solution {
256
256
```
257
257
258
258
Python:
259
-
259
+ ``` python
260
+ # Definition for a binary tree node.
261
+ # class TreeNode:
262
+ # def __init__(self, val=0, left=None, right=None):
263
+ # self.val = val
264
+ # self.left = left
265
+ # self.right = right
266
+ // 递归法
267
+ class Solution :
268
+ def constructMaximumBinaryTree (self , nums : List[int ]) -> TreeNode:
269
+ if not nums: return None // 终止条件
270
+ root = TreeNode(max (nums)) // 新建节点
271
+ p = nums.index(root.val) // 找到最大值位置
272
+ if p > 0 : // 保证有左子树
273
+ root.left = self .constructMaximumBinaryTree(nums[:p]) // 递归
274
+ if p < len (nums): // 保证有右子树
275
+ root.right = self .constructMaximumBinaryTree(nums[p+ 1 :]) // 递归
276
+ return root
277
+ ```
260
278
261
279
Go:
262
280
267
285
* 作者微信:[ 程序员Carl] ( https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw )
268
286
* B站视频:[ 代码随想录] ( https://space.bilibili.com/525438321 )
269
287
* 知识星球:[ 代码随想录] ( https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ )
270
- <div align="center"><img src=../pics/公众号.png width=450 alt=> </img ></div >
288
+ <div align="center"><img src=../pics/公众号.png width=450 alt=> </img ></div >
You can’t perform that action at this time.
0 commit comments