538. Convert BST to Greater Tree
题目
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。
示例
输入: 二叉搜索树:
5
/ \
2 13
输出: 转换为累加树:
18
/ \
20 13
思路
概念
BST的定义
如果一个二叉树满足:对于任意一个节点,其值不小于左子树的任何节点,且不大于右子树的任何节点(反之亦可),则为二叉搜索树。若按照中序遍历,其遍历结果是一个有序序列。
前中后序遍历
前中后指的是父节点在前中后。以一个最基本的二叉树为例,前序是根左右,中序是左根右,后序是左右根。
本题思路
由于这是一个BST,若中序遍历的话是一个从小到大的序列。我们这道题要求的意思是把每个节点都加上它在这个序列中后面的所有数的和。实现上类似于二叉树的遍历,对于本题我们要的顺序是右根左。设置一个total记录要加给当前节点的总和,代码如下:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def __init__(self):
self.total = 0
def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root is not None:
self.convertBST(root.right)
#self.total += root.val
temp = root.val
root.val += self.total
self.total += temp
self.convertBST(root.left)
return root
时间复杂度,由于是遍历,O(n)
空间复杂度,O(n)
栈迭代(Solution)
class Solution(object):
def convertBST(self, root):
total = 0
node = root
stack = []
while stack or node is not None:
# push all nodes up to (and including) this subtree's maximum on
# the stack.
while node is not None:
stack.append(node)
node = node.right
node = stack.pop()
total += node.val
node.val = total
# all nodes with values between the current and its parent lie in
# the left subtree.
node = node.left
return root
Morris逆转顺序遍历(译自Solution)
直觉
有一种聪明的方法可以只使用线性时间和常量空间来执行顺序遍历,这是J. H. Morris在1979年的论文《简单而廉价地遍历二叉树》中首次描述的。通常,递归和迭代堆栈方法牺牲了线性空间,以便在访问节点的左子树之后返回节点。Morris遍历利用树的叶子中未使用的空指针在左子树中创建一个临时链接,只允许使用常量附加内存执行遍历。要将其应用于此问题,只需交换所有“左”和“右”引用,这将逆转遍历。
算法
首先,我们初始化指向根的节点。然后,直到节点指向null(特别是树的最小值节点的左null),我们重复以下操作。首先,考虑当前节点是否具有正确的子树。如果它没有右子树,那么就没有值更大的未访问节点,所以我们可以访问这个节点并移动到左子树。如果它有一个正确的子树,那么至少有一个未访问的节点具有更大的值,因此我们必须首先访问正确的子树。为此,我们通过帮助函数get继承者获得对顺序继承的引用(比当前节点大的最小值节点)。这个继承节点是必须在当前节点之前立即访问的节点,因此根据定义,它有一个空左指针(否则它就不是继承节点)。因此,当我们第一次找到一个节点的后续节点时,我们临时将它(通过它的左指针)链接到该节点,并继续到该节点的右子树。然后,当我们完成对右子树的访问时,其中最左边的指针将是我们可以用来转义子树的临时链接。在跟踪这个链接之后,我们回到了之前经过但没有访问的原始节点。这一次,当我们发现继承节点的左指针循环回到当前节点时,我们知道我们已经访问了整个右子树,因此我们现在可以删除临时链接并移动到左子树中。
class Solution(object):
def convertBST(self, root):
# Get the node with the smallest value greater than this one.
def get_successor(node):
succ = node.right
while succ.left is not None and succ.left is not node:
succ = succ.left
return succ
total = 0
node = root
while node is not None:
# If there is no right subtree, then we can visit this node and
# continue traversing left.
if node.right is None:
total += node.val
node.val = total
node = node.left
# If there is a right subtree, then there is a node that has a
# greater value than the current one. therefore, we must traverse
# that node first.
else:
succ = get_successor(node)
# If there is no left subtree (or right subtree, because we are
# in this branch of control flow), make a temporary connection
# back to the current node.
if succ.left is None:
succ.left = node
node = node.right
# If there is a left subtree, it is a link that we created on
# a previous pass, so we should unlink it and visit this node.
else:
succ.left = None
total += node.val
node.val = total
node = node.left
return root