美文网首页Leetcode
Leetcode - 538. Convert BST to G

Leetcode - 538. Convert BST to G

作者: KkevinZz | 来源:发表于2017-03-20 04:18 被阅读0次

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


相关文章

网友评论

    本文标题:Leetcode - 538. Convert BST to G

    本文链接:https://www.haomeiwen.com/subject/zycbnttx.html