Merge pull request #225 from jojoo15/patch-19

添加 0538.把二叉搜索树转换为累加树 python3版本
This commit is contained in:
Carl Sun
2021-05-22 00:31:52 +08:00
committed by GitHub

View File

@ -196,8 +196,26 @@ class Solution {
```
Python
```python3
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
#递归法
class Solution:
def convertBST(self, root: TreeNode) -> TreeNode:
def buildalist(root):
if not root: return None
buildalist(root.right) #右中左遍历
root.val += self.pre
self.pre = root.val
buildalist(root.left)
self.pre = 0 #记录前一个节点的数值
buildalist(root)
return root
```
Go