Leetcode - 538. Convert BST to Greater Tree


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




最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容