Convert BST to Greater Tree
Initialize variables
Initialize the accumulated sum to 0, create an empty stack, and set the current node to the root (node with value 4).
acc_sum = 0
stack = []
node = rootTraverse right subtree from root
Push the current node (4) onto the stack and move to its right child (6) to reach the rightmost node first.
while node:
stack.append(node)
node = node.rightPush right child (7) and move to its right child (8)
Push node 7 onto the stack and move to its right child 8, continuing to go as far right as possible.
while node:
stack.append(node)
node = node.rightProcess rightmost node (8)
Pop node 8 from the stack, add its value (8) to acc_sum (0), update node 8's value to 8, then move to its left child (null).
node = stack.pop()
acc_sum += node.val
node.val = acc_sum
node = node.leftBack to node 7, process it
Pop node 7 from the stack, add its value (7) to acc_sum (8), update node 7's value to 15, then move to its left child (null).
node = stack.pop()
acc_sum += node.val
node.val = acc_sum
node = node.leftBack to node 6, process it
Pop node 6 from the stack, add its value (6) to acc_sum (15), update node 6's value to 21, then move to its left child (5).
node = stack.pop()
acc_sum += node.val
node.val = acc_sum
node = node.leftTraverse left child of node 6 (node 5)
Move to node 5, push it onto the stack, and since it has no right child, prepare to process it.
while node:
stack.append(node)
node = node.rightProcess node 5
Pop node 5 from the stack, add its value (5) to acc_sum (21), update node 5's value to 26, then move to its left child (null).
node = stack.pop()
acc_sum += node.val
node.val = acc_sum
node = node.leftBack to node 3, process it
Pop node 3 (originally 4) from the stack, add its value (4) to acc_sum (26), update node 3's value to 30, then move to its left child (1).
node = stack.pop()
acc_sum += node.val
node.val = acc_sum
node = node.leftTraverse right subtree of node 1 (node 2)
Push node 1 onto the stack and move to its right child (2), then push node 2 and move to its right child (3).
while node:
stack.append(node)
node = node.rightProcess node 3
Pop node 3 from the stack, add its value (3) to acc_sum (30), update node 3's value to 33, then move to its left child (null).
node = stack.pop()
acc_sum += node.val
node.val = acc_sum
node = node.leftBack to node 2, process it
Pop node 2 from the stack, add its value (2) to acc_sum (33), update node 2's value to 35, then move to its left child (null).
node = stack.pop()
acc_sum += node.val
node.val = acc_sum
node = node.leftTraverse left child of node 2 (node 0)
Move to node 0, push it onto the stack. Since it has no right child, prepare to process it.
while node:
stack.append(node)
node = node.rightProcess node 0
Pop node 0 from the stack, add its value (0) to acc_sum (35), update node 0's value to 35, then move to its left child (null).
node = stack.pop()
acc_sum += node.val
node.val = acc_sum
node = node.leftTraversal complete, return updated root
Stack is empty and current node is null, traversal is complete. Return the root node with updated values.
return rootclass TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def convertBST(root):
acc_sum = 0 # STEP 1
stack = [] # STEP 1
node = root # STEP 1
while stack or node: # STEP 2
while node: # STEP 2
stack.append(node) # STEP 2
node = node.right # STEP 2
node = stack.pop() # STEP 4
acc_sum += node.val # STEP 4
node.val = acc_sum # STEP 4
node = node.left # STEP 5
return root # STEP 6
# Driver code
if __name__ == '__main__':
root = TreeNode(4,
TreeNode(1,
TreeNode(0),
TreeNode(2, None, TreeNode(3))),
TreeNode(6,
TreeNode(5),
TreeNode(7, None, TreeNode(8))))
new_root = convertBST(root)
def inorder(node):
return inorder(node.left) + [node.val] + inorder(node.right) if node else []
print(inorder(new_root))Key Takeaways
This traversal order ensures each node's new value includes all greater values, which is hard to see from code alone.
Watching the stack grow and shrink clarifies how the algorithm backtracks and processes nodes in correct order.
Seeing values change step-by-step helps understand how the accumulated sum affects each node.
