【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 ...
随机推荐
- pc显示,手机隐藏
<div class="none"><img src="https://www.foresthouse.cn/bigpic.jpg"/> ...
- python实现格式化输出9*9乘法表
# python 9*9 乘法表 for i in range(1,10): for j in range(1,i+1): print("%s*%s=%s"%(i,j,i*j),e ...
- SQLServer中的top、MySql中的limit、Oracle中的rownum
(1)在SQL Server中,我们使用 select top N * from tablename来查询tablename表中前N条记录. (2)在MySQL中,我们使用select * from ...
- jdbcTemplate和namedParameterJdbcTemplate
jdbcTemplatejdbcTemplate配置<!-- 注入jdbcTemplate 官方工具包 --> <bean id="jdbc" class=&qu ...
- Java ——Number & Math 类 装箱 拆箱 代码块
本节重点思维导图 当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等 int a = 5000; float b = 13.65f; byte c = 0 ...
- Common Linux Commands 日常工作常用Linux命令
How to know CPU info cat /proc/cpuinfo arch How to know memory info: cat /proc/meminfo ...
- [13期]mysql-root全手工注入写马实例实战
回显方便的小工具
- 多线程04-ThreadPriority
; i < ; i++) { Thread.Sleep(); Console.WriteLine( ...
- CentOS 添加硬盘创建并挂载分区
分区工具介绍: fdisk 创建MBR分区:所支持的最大卷:2T,而且对分区有限制:最多4个主分区或3个主分区加一个扩展分区 gdisk 创建GPT分区:突破MBR 4个主分区限制,每个磁盘最多支持1 ...
- C# DataSet转JSON
经常会遇到系统数据交互采用JSON数据格式进行交互的,避免不必要的重复工作,记录下自己的处理方式. 获取数据集之后,通过函数对数据集信息进行整理通过.Net Framework3.5提出的JavaSc ...
