其他分享
首页 > 其他分享> > 刷题记录:538. 把二叉搜索树转换为累加树

刷题记录:538. 把二叉搜索树转换为累加树

作者:互联网

538. 把二叉搜索树转换为累加树

# 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]:
        _sum = 0

        def inorderTravese_Reverse(root):
            if not root:
                return
            else:
                inorderTravese_Reverse(root.right)
                nonlocal _sum
                _sum += root.val
                root.val = _sum
                inorderTravese_Reverse(root.left)

        inorderTravese_Reverse(root)

        return root

标签:right,Reverse,val,self,二叉,inorderTravese,刷题,root,538
来源: https://blog.csdn.net/Peloponnesian/article/details/122720481