题目如下:

Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val.

As a reminder, a binary search tree is a tree that satisfies these constraints:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

Input: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Note:

  1. The number of nodes in the tree is between 1 and 100.
  2. Each node will have value between 0 and 100.
  3. The given tree is a binary search tree.

解题思路:我的方法简单粗暴,第一次遍历树,把树中每个节点的值存入list;接下来再遍历一次,对于每个node,在list中找出所有值比自己大的元素的和,加上node自身的值,即为这个node的新值。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
val_list = []
def recursive(self,node):
self.val_list.append(node.val)
if node.right != None:
self.recursive(node.right)
if node.left != None:
self.recursive(node.left) def reValue(self,node):
inx = self.val_list.index(node.val)
node.val += sum(self.val_list[inx+1:])
if node.right != None:
self.reValue(node.right)
if node.left != None:
self.reValue(node.left) def bstToGst(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root != None:
self.val_list = []
self.recursive(root)
self.val_list.sort()
self.reValue(root)
return root

【leetcode】1038. Binary Search Tree to Greater Sum Tree的更多相关文章

  1. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  2. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  3. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  4. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  6. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. 【题解】【BST】【Leetcode】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. 【leetcode】 Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

随机推荐

  1. 自定义控件 - 流式布局:TagFlowLayout

    在项目中需要用到流式布局的样式,此文学习鸿洋大神的FlowLayout控件,学习使用一下.出自 http://blog.csdn.net/lmj623565791/article/details/38 ...

  2. Linux基础—saltstack运维工具学习

    一.saltstack简介 1.saltstack是什么 系统管理员日常会进行大量的重复性操作,例如安装软件,修改配置文件,创建用户,批量执行命令等,如果主机数量庞大,单靠人工维护实在让人难以忍受. ...

  3. centos输入正确的账号和密码登陆不进去

    vm下启动centos,输入正确的账号和密码,依然登陆不进去,一直处于这个界面: 暂时的解决方法是:先等待一段时间.重启,然后再输入密码,然后,ctrl+c 不停地ctrl+c,然后就登陆进去了.什么 ...

  4. 【ABAP系列】SAP ABAP 利用class创建客户/供应商主数据

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 利用class创建 ...

  5. [Web 前端] 002 html 常用行行级元素

    目录 1. html 常用的行级元素 1.1 链接标签 1.2 a 标签的锚点的使用 1.3 文本标签 1.4 无语义的行级元素 span 1.5 html 中的实体字符 1. html 常用的行级元 ...

  6. Java:CAS(乐观锁)

    本文讲解CAS机制,主要是因为最近准备面试题,发现这个问题在面试中出现的频率非常的高,因此把自己学习过程中的一些理解记录下来,希望能对大家也有帮助. 什么是悲观锁.乐观锁?在java语言里,总有一些名 ...

  7. 在react中用装饰器来装饰connect

    假设我们在react中有如下header组件: import React, { PureComponent } from 'react'; import { connect } from 'react ...

  8. vue+element Form键盘回车事件页面刷新解决

    问题描述:如下代码所示,使用element-ui 中的el-form组件对table进行条件查询,当查询条件仅有一项时,使用@keyup.enter.native事件绑定回车事件,出现点击回车后,浏览 ...

  9. 浏览器是怎样工作的:渲染引擎,HTML解析(连载二)

    转载自:http://ued.ctrip.com/blog/how-browsers-work-rendering-engine-html-parsing-series-ii.html 渲染引擎 渲染 ...

  10. Git基本常用指令

    开发十年,就只剩下这套架构体系了! >>>   Git基本常用命令如下: mkdir:         XX (创建一个空目录 XX指目录名) pwd:          显示当前目 ...