538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

Example:

Input: The root of a Binary Search Tree like this:
              5
            /              2     13

Output: The root of a Greater Tree like this:
             18
            /             20     13

题意:给出二叉搜索树,每一个节点累加比自己更大的值

解法:根据二叉搜索树的特性可知,按照 右->根->左的顺序遍历节点

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution(object):
  8. def convertBST(self, root):
  9. """
  10. :type root: TreeNode
  11. :rtype: TreeNode
  12. """
  13. self.helper(root)
  14. return root
  15. sum = 0
  16. def helper(self, node):
  17. if(node):
  18. self.helper(node.right)
  19. node.val += self.sum
  20. self.sum = node.val
  21. self.helper(node.left)

来自为知笔记(Wiz)

时间: 2024-10-06 13:24:41

538. Convert BST to Greater Tree 二叉搜索树转换为更大树的相关文章

13.Convert BST to Greater Tree(将树转为更大树)

Level: ??Easy 题目描述: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary

538 Convert BST to Greater Tree 把二叉搜索树转换为累加树

给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树:              5            /   \           2     13输出: 转换为累加树:             18            /   \          20     13详见:https://leetcode.com/problems/convert-

538. Convert BST to Greater Tree

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

[LeetCode] 538. Convert BST to Greater Tree

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

538-把二叉搜索树转换为累加树

538-把二叉搜索树转换为累加树 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉搜索树: 5 / 2 13 输出: 转换为累加树: 18 / 20 13 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/convert-bst-to-greater-tree 著作权归领扣网络所有.商业转载请联系官方授权

把二叉搜索树转化成更大的树 · Convert BST to Greater Tree

[抄题]: 给定二叉搜索树(BST),将其转换为更大的树,使原始BST上每个节点的值都更改为在原始树中大于等于该节点值的节点值之和(包括该节点). Given a binary search Tree `{5,2,13}`: 5 / 2 13 Return the root of new tree 18 / 20 13 [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 反向求和并把和赋给root.val [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常

LeetCode算法题-Convert BST to Greater Tree(Java实现)

这是悦乐书的第255次更新,第268篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第122题(顺位题号是538).给定二进制搜索树(BST),将其转换为更大树,使原始BST的每个键都更改为原始键加上所有键的总和大于BST中的原始键.例如: 输入:二进制搜索树的根,如下所示: 5 / 2 13 输出:大树的根,如下所示: 18 / 20 13 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 0

LeetCode 把二叉搜索树转换为累加树(538)

第538题 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉搜索树: 5 / 2 13 输出: 转换为累加树: 18 / 20 13 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/convert-bst-to-greater-tree 概念 二叉搜索树 二叉查找树(Binary Search Tree

[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has