tag 上說是 tree 類型的題目岖食,但我覺得這更像是一個 backtracking的題目
解法:
因為題目要求所有比node.val大得值都要加上去曹体,而這是一顆BST色瘩,所以比node.val大的值肯定都在node的右邊单绑,
base:
if is_leaf(node):
? ? ?add the value of node into carry
? ? set the value of the node = to carry
step:
if there is right node
'order is important here, you have to go to right firstto allocate all value greater than node.val' recursion(root.right,carry)
if there is left node:
since all left node is sure smaller than node.val, all we need to do here is simpliy add the number into all left node'val