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 BST is changed to the original key plus sum of all keys greater than the original key in BST.
题目分析及思路
题目给出一棵二叉搜索树,要求将它转成Greater Tree。定义Greater Tree的结点值为原树中的结点值加上所有比该结点值大的结点值。可使用递归方法,顺序为右根左,需定义一个全局的变量统计结点值的和。
python代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def convertBST(self, root: TreeNode) -> TreeNode:
self.s = 0
def treesum(node):
if not node:
return
treesum(node.right)
node.val += self.s
self.s = node.val
treesum(node.left)
treesum(root)
return root
LeetCode 538 Convert BST to Greater Tree 解题报告的更多相关文章
- 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 (把二叉搜索树转换成较大树)
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 original B ...
- 【leetcode_easy】538. Convert BST to Greater Tree
problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完
- 【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 ...
- 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 ...
- 538 Convert BST to Greater Tree 把二叉搜索树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树: ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
随机推荐
- 【原创】大叔经验分享(3)hbase client 如何选择
java中访问hbase有两种方式,一种是hbase自带的client,一种是通过hbase thrift 1 hbase client示例 Configuration conf = HBaseCon ...
- PHP源码安装后设置别名
PHP源码安装后测试是否能正常运行 每次在php目录./bin./php调用php很不方便,可以设置别名(方法一) vi ~/.bash_profile (修改根目录下这个文件) 设置完成后还 ...
- niagara Workbench module import IntelliJ
1. 在整个网络布线中使用一种布线方式,但是两端都有RJ-45 plug 的网络连线,无论是采用方式A还是方式B 端接的方式都是试用的.网络都是通用的,双绞线的顺序与RJ-45偶的引脚序号一一对应, ...
- dynamic不能使用扩展方法
一.方法中传入的参数为dynamic,则返回的也是dynamic类型 例如: public string Test(string str) { return str + "aa"; ...
- zt 正则
http://deerchao.net/tutorials/regex/regex.htm 正则表达式30分钟入门教程 版本:v2.3.5 (2017-6-12) 作者:deerchao 转载 ...
- docker 搭建以太坊私有链搭建
最近区块链,火得不行,身边也有朋友准备玩这个,说是搭了一个星期,没有把环境搭建起来,叫我帮忙看看环境怎么搭建 于是我找到了官方的地址 https://github.com/ethereum/go-et ...
- tensorflow激励函数-【老鱼学tensorflow】
当我们回到家,如果家里有异样,我们能够很快就会发现家中的异样,那是因为这些异常的摆设在我们的大脑中会产生较强的脑电波. 当我们听到某个单词,我们大脑中跟这个单词相关的神经元会异常兴奋,而同这个单词无关 ...
- HDU 5984.Pocky(2016 CCPC 青岛 C)
Pocky Let’s talking about something of eating a pocky. Here is a Decorer Pocky, with colorful decora ...
- python 单行写法
if not any([_ in fingers for _ in finger_ids])
- Linux 安装 JDK 8
1. step1: 下载最新的 Java Archive # wget --no-cookies --no-check-certificate --header "Cookie: gpw_ ...