Skip to content

Commit f7828e6

Browse files
authored
添加 0538.把二叉搜索树转换为累加树 python3版本
添加 0538.把二叉搜索树转换为累加树 python3版本
1 parent fa76dfe commit f7828e6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

problems/0538.把二叉搜索树转换为累加树.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,26 @@ class Solution {
196196
```
197197

198198
Python:
199-
200-
199+
```python3
200+
# Definition for a binary tree node.
201+
# class TreeNode:
202+
# def __init__(self, val=0, left=None, right=None):
203+
# self.val = val
204+
# self.left = left
205+
# self.right = right
206+
#递归法
207+
class Solution:
208+
def convertBST(self, root: TreeNode) -> TreeNode:
209+
def buildalist(root):
210+
if not root: return None
211+
buildalist(root.right) #右中左遍历
212+
root.val += self.pre
213+
self.pre = root.val
214+
buildalist(root.left)
215+
self.pre = 0 #记录前一个节点的数值
216+
buildalist(root)
217+
return root
218+
```
201219
Go:
202220

203221

@@ -207,4 +225,4 @@ Go:
207225
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
208226
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
209227
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
210-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
228+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

0 commit comments

Comments
 (0)