题目要求

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.

题目分析及思路

题目给出一棵二叉搜索树,要求将它转成Greater Tree。定义Greater Tree的结点值为原树中的结点值加上所有比该结点值大的结点值。可使用递归方法,顺序为右根左,需定义一个全局的变量统计结点值的和。

python代码

# Definition for a binary tree node.

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

def convertBST(self, root: TreeNode) -> TreeNode:

self.s = 0

def treesum(node):

if not node:

return

treesum(node.right)

node.val += self.s

self.s = node.val

treesum(node.left)

treesum(root)

return root

LeetCode 538 Convert BST to Greater Tree 解题报告的更多相关文章

  1. 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  2. LN : leetcode 538 Convert BST to Greater Tree

    lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...

  3. 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 B ...

  4. [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 B ...

  5. 【leetcode_easy】538. Convert BST to Greater Tree

    problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完

  6. 【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 orig ...

  7. 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 B ...

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

    给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树:     ...

  9. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

随机推荐

  1. C++设计模式——模板方法模式

    模板方法模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对模板方法模式是这样说的:定义一个操作中的算法骨架,而将一些步骤延迟到子类中.TemplateMethod使得子类可以不 ...

  2. 【web】服务器推送的实现方式(转)

    轮询 http流 websocket   https://www.cnblogs.com/freud/p/8397934.html http2.0 浅谈Websocket.Ajax轮询和长连接(lon ...

  3. Shell入门(一)

    一.Shell参数变量相关知识: $@: 所有参数,每个参数带双引号.以"$1" "$2" "$3"的形式出现, $*:  所有参数,所有参 ...

  4. Python判断水仙花数

    水仙花数 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿姆斯特朗数( ...

  5. Web微信模拟

    一.概要 目的:实现一个具有web微信类似功能的项目 框架:Django 模块:render.HttpResponse.BeautifulSoup.re.time.requests.json.rand ...

  6. java请求参数转换

    public class HttpServletRequestUtil { //转换请求参数 public static int getInt(HttpServletRequest request, ...

  7. SSL通信-忽略证书认证错误

    .NET的SSL通信过程中,使用的证书可能存在各种问题,某种情况下可以忽略证书的错误继续访问.可以用下面的方式跳过服务器证书验证,完成正常通信. 1.设置回调属性ServicePointManager ...

  8. requests之headers 'Content-Type': 'text/html'误判encoding为'ISO-8859-1'导致中文text解码错误

    0. requests不设置UA 访问baidu 得到 r.headers['Content-Type'] 是text/html  使用chrome UA: Content-Type:text/htm ...

  9. numpy的基础运算-【老鱼学numpy】

    概述 本节主要讲解numpy数组的加减乘除四则运算. np.array()返回的是numpy的数组,官方称为:ndarray,也就是N维数组对象(矩阵),N-dimensional array obj ...

  10. matplotlib图例-【老鱼学matplotlib】

    图例是啥,直接上图就知道了: 怎么创建上面的图例呢? 很简单,首先在plt.plot()函数中设置label文本属性,然后调用plt.legend()生成图例就可以了,完整的代码如下: import ...