mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #225 from jojoo15/patch-19
添加 0538.把二叉搜索树转换为累加树 python3版本
This commit is contained in:
@ -196,8 +196,26 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
Go:
|
||||||
|
|
||||||
|
|
||||||
@ -207,4 +225,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
Reference in New Issue
Block a user