应用题-二叉树专题-把二叉搜索树转换为累加树

管理员
```python # 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: Optional[TreeNode]) -> Optional[TreeNode]: # 很显然 叠加顺序 右根左 self.value = 0 def accumulate( r ): if not r: return accumulate( r.right ) r.val = r.val + self.value self.value = r.val accumulate( r.left ) accumulate( root ) return root ```
评论 0

发表评论 取消回复

Shift+Enter 换行  ·  Enter 发送
还没有评论,来发表第一条吧