Leetcode

Leetcode - 538. Convert BST to G

2017-03-20  本文已影响0人  KkevinZz

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


上一篇 下一篇

猜你喜欢

热点阅读