【leetcode】1038. Binary Search Tree to Greater Sum Tree
题目如下:
Given the root of a binary search tree with distinct values, modify it so that every
nodehas a new value equal to the sum of the values of the original tree that are greater than or equal tonode.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:
- The number of nodes in the tree is between
1and100.- Each node will have value between
0and100.- 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的更多相关文章
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【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 ...
- 【题解】【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 ...
- 【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 ...
- 【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
随机推荐
- 【MM系列】SAP ABAP BAPI 和 RFC 的区别
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP BAPI 和 ...
- JSP程序不能正常运行 MyEclipse10 Tomcat6.0
我写的sp程序,上午运行正常:但是下午再打开运行会提示对jsp解释失败 谁知道这是怎么回事呢? 后来是发现: 要运行JSP程序 Myeclipse10和Tomcat6的jdk都要调整到jdk1.7的版 ...
- [SDOI2016]征途 —— 斜率优化DP
时隔多年没有碰斜率优化了... 想当年被斜率优化虐的死去活来,现在看看...也就那样吧. Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计 ...
- vue中的provide/inject讲解
最近在看element-ui的源码,发现了一个这样的属性:inject.遂查看官网provider/inject provider/inject:简单的来说就是在父组件中通过provider来提供变量 ...
- debian下重装mysql
mysql总是报错,说sock文件不存在,网上若干方法,更改权限,更改配置文件,结果还是不能正常生成.sock文件.没办法,删除,重新安装. 完全删除: 删除 mysqlsudo apt-get au ...
- mooc-IDEA 收藏位置和文件--003
六.IntelliJ IDEA -收藏位置和文件(类/函数) 1.收藏自己喜欢的文件---代码 添加一个Favorites列表 定义名称 Help->Find Action... 选择Add t ...
- Web前端开发 --》 如何实现页面同时在移动端和pc端的兼容问题
很简单,只需要在html文件中对你引入的css进行一个类似于媒体查询的操作 <!DOCTYPE html> <html lang="en"> <hea ...
- ntp局域网时间同步操作
需求:局域网里面有两台电脑需要同步时间 一台windows,一台Linux.把windows当作服务器 windows10自带ntp服务器,可以按如下步骤进行设置 1. 打开注册表编辑器,在运行里面输 ...
- javascript数组排序和prototype详解
原型的概念::原型对象里的所有属性和方法 被所有构造函数实例化出来的对象所共享,类似于java中的 static 正因为共享所以单一的操作 就会影响了全局,因此使用时需注意 基于prototype:为 ...
- 懵圈了,面试官问一个 TCP 连接可发多少个 HTTP 请求?
作者:松若章 https://zhuanlan.zhihu.com/p/61423830 一道经典的面试题是从 URL 在浏览器被被输入到页面展现的过程中发生了什么,大多数回答都是说请求响应之后 DO ...
