题目如下:

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

解题思路:一时卡壳了,只想出了一个很挫的方法,把所有节点的值都取出来,然后找出每个节点的所有比其大的值的和,这个值就是这个节点的值要加上的大小,最后再遍历一次树修改节点。

代码如下:

# 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):
l = []
dic = {}
def traverse(self,node,mode):
if node == None:
return
if mode == 1:
self.l.append(node.val)
else:
node.val += self.dic[node.val]
if node.left != None:
self.traverse(node.left,mode)
if node.right != None:
self.traverse(node.right,mode) def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
self.l = []
self.dic = {}
self.traverse(root,1)
ul = sorted(list(set(self.l)))
total = 0
for i in ul[::-1]:
self.dic[i] = total
total += i
self.traverse(root, 0)
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. 【leetcode_easy】538. Convert BST to Greater Tree

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

  3. 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), ...

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

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

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

    这是悦乐书的第255次更新,第268篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第122题(顺位题号是538).给定二进制搜索树(BST),将其转换为更大树,使原始BS ...

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

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

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

随机推荐

  1. 【串线篇】SpringMvc视图解析

    一. 请求处理方法执行完成后,最终返回一个 ModelAndView 对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它们装配成一个 ...

  2. 西电源ubuntu12

    deb http://linux.xidian.edu.cn/mirrors/ubuntu/ precise main restricted universe multiverse #deb-src ...

  3. 怀旧浪潮来袭,小霸王游戏、windows95......曾经的经典哪些能戳中你的心怀?

    随着前两天上架的 Rewound 在 iPhone 上复刻了 iPod Classic为大家掀起一场怀旧浪潮,那么除了 Rewound还有什么经典?今天我们就来怀旧一下那些曾经的经典.80经典小霸王游 ...

  4. layui导出表格全部数据

    layui自带的导出表格,只能导出当前页面,如果当前页包含全部数据,那不就是导出全部数据了吗,所以我给导出事件单独定义了一个请求,当触发这个请求时,在后台查询数据时不要按接收的page 和 limit ...

  5. jquery实现回车键登录/搜索等确认功能

    button按钮提交方式: $('#search').click(function() { get_table(); }); //keyCode=13是回车键,设置回车键提交 $("body ...

  6. context和getApplicationContext()的区别

    在android中常常会遇到与context有关的内容 浅论一下 context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder ...

  7. 自定义缓存管理器 或者 Spring -- cache

    Spring Cache 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 c ...

  8. 四. jenkins部署springboot项目(1)--window环境

    前提:jenkins和springboot运行在同一台机器 springboot项目使用git和maven jenkins所需的插件如Maven,Git等这里就不再详述. 1.jenkins配置git ...

  9. JdbcTemplate 的oracle分页

    @Autowired private JdbcTemplate jd; int ps1=Integer.valueOf(pageSize); int cp1=Integer.valueOf(currP ...

  10. spring相关注解

    spring相关注解: 使用之前需要<context:annotation-config/>在配置文件中启用 @Required 应用于类属性的set方法,并且要求该属性必须在xml容器里 ...