[LeetCode&Python] Problem 860. Convert BST to Greater Tree
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):
'''
def __init__(self):
self.total=0 def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root:
return root
self.convertBST(root.right)
self.total+=root.val
root.val=self.total
self.convertBST(root.left)
return root
'''
def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
""" total=0
stack=[]
node=root
while node is not None or stack:
while node is not None:
stack.append(node)
node=node.right
node=stack.pop()
total+=node.val
node.val=total
node=node.left
return root
[LeetCode&Python] Problem 860. Convert BST to Greater Tree的更多相关文章
- 【leetcode_easy】538. Convert BST to Greater Tree
problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完
- 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), ...
- 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 ...
- [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- 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 ...
- [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 ...
- LeetCode - 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 ...
- 【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 ...
随机推荐
- 小程序证书申请FAQ
1. 帮别人开发小程序, 先把你的微信号加到成员里, 并给予开发者权限,体验者权限,登录,数据分析,开发管理,开发设置 2. 需要https, 不能用windows2003,必须2008以上,用IIS ...
- shiro学习笔记-Subject#login(token)源码实现过程
追踪Subject的login(AuthenticationToken token)方法,其调用的为DelegatingSubject类的login方法,DelegatingSubject实现了Sub ...
- MyBatis 为什么需要通用 Mapper ?
一.通用 Mapper 的用途 ? 我个人最早用 MyBatis 时,先是完全手写,然后用上了 MyBatis 代码生成器(简称为 MBG),在使用 MBG 过程中,发现一个很麻烦的问题,如果数据库字 ...
- Resharper插件的使用
一.Resharper设置 1.1 智能提示 安装完毕后,IDE 的智能提示(Intellisense)便会默认使用 Resharper 的提示,不知道为什么,我一直不太喜欢它的提示.改过来,是在Op ...
- bzoj2440
题解: 莫比乌斯反演 ans=sigma(x/(i*i)*miu[i]) 代码: #include<bits/stdc++.h> using namespace std; ; int T, ...
- day04 列表
今天主要学习了 列表 什么是列表 定义: 能装对象的对象 在python中使用[]来描述列表, 内部元素用逗号隔开. 对数据类型没有要求 列表存在索引和切片. 和字符串是一样的. 2. 相关的增删改查 ...
- PHP多进程处理并行处理任务实例
本文目的 本文通过例子讲解linux环境下,使用php进行并发任务处理,以及如何通过pipe用于进程间的数据同步.写得比较简单,作为备忘录. PHP多进程 通过pcntl_XXX系列函数使用多进程功能 ...
- bs4 CSS选择器
#https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all #beautifulSoup可以解析HTML ...
- layer 问题 汇总
1.搭配iframe 子页面遮罩层 覆盖父页面 window.parent.layer.open({ // type: 1, //skin: 'layui-layer-rim', //加上 ...
- <Parquet><Physical Properties><Best practice><With impala>
Parquet Parquet is a columnar storage format for Hadoop. Parquet is designed to make the advantages ...