Python | Leetcode Python题解之第538题把二叉搜索树转换为累加树
题目:
题解:
class Solution:def convertBST(self, root: TreeNode) -> TreeNode:def getSuccessor(node: TreeNode) -> TreeNode:succ = node.rightwhile succ.left and succ.left != node:succ = succ.leftreturn succtotal = 0node = rootwhile node:if not node.right:total += node.valnode.val = totalnode = node.leftelse:succ = getSuccessor(node)if not succ.left:succ.left = nodenode = node.rightelse:succ.left = Nonetotal += node.valnode.val = totalnode = node.leftreturn roota