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的更多相关文章

  1. 【leetcode_easy】538. Convert BST to Greater Tree

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

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

  3. 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

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

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

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

  9. 【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 ...

随机推荐

  1. 【转】Vue-详解设置路由导航的两种方法: <router-link :to="..."> 和router.push(...)

    一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串 <router-link to= ...

  2. 漫谈moosefs中cgi各项的意义

    原创:http://www.cnblogs.com/bugutian/p/6869278.html 转载请注明出处 一.先上一张图 二.解释 1. Metadata Servers (masters) ...

  3. docker安装使用教程(Kali2.0)

    一.apt安装 apt直接安装是最好的,因为apt源中的其他docker相关组件,也是与docker匹配的版本. apt-get install docker docker-compose 二.手动安 ...

  4. maven plugins

    <build> <finalName>lessons</finalName> <plugins> <plugin> <groupId& ...

  5. Dagger2不自动生成daggerXXXcomponent

    在Fragment里面初始化dagger2创建对象时,不自动生成daggerXXXcomponent. 百思不得其解,后来发现是import android.app.Fragment;所以不自动生成. ...

  6. windows 下的命令操作

    删除文件夹 RD /S D:\aaaaa 删除文件夹下的文件 DEL D:\aaaaa\*.*

  7. sqlcipher 数据库解密

    使用 sqlcipher.exe 可以在输入密码后,查看加密数据库的内容. 但是要编码查询数据库的内容,还要另寻方法.(相关的工具和库在我的百度网盘中) 使用sqlcipher windows 命令工 ...

  8. MyEclipse常用设置和快捷键

    Java快捷键 1.注释快捷键 先敲/  再敲两个**     Enter 回车 2.system.out.println(); 常用设置 [子类继承父类] [编码字体设置] 删除当前行:ctrl+d ...

  9. netty]--最通用TCP黏包解决方案

    netty]--最通用TCP黏包解决方案:LengthFieldBasedFrameDecoder和LengthFieldPrepender 2017年02月19日 15:02:11 惜暮 阅读数:1 ...

  10. 使用perfect进行服务端开发

    最近闲来无事,研究了下基于perfect的swift后端开发.根据大神的博客进行了简单的配置,加深下印象也算是和各位分享一下. 参考博客:http://www.cnblogs.com/ludashi ...