leetcode538
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
Stack<TreeNode> S = new Stack<TreeNode>(); private void postNode(TreeNode node)
{
if (node != null)
{
if (node.left != null)
{
postNode(node.left);
}
S.Push(node);
if (node.right != null)
{
postNode(node.right);
} }
} public TreeNode ConvertBST(TreeNode root)
{
postNode(root);
var list = S.ToList();
var sum=; foreach (var l in list)
{
sum += l.val;
l.val = sum;
} return root;
}
}
https://leetcode.com/problems/convert-bst-to-greater-tree/#/description
补充一个python的实现:
class Solution(object):
def __init__(self):
self.total = def convertBST(self, root):
if root is not None:
self.convertBST(root.right)
self.total += root.val
root.val = self.total
self.convertBST(root.left)
return root
leetcode538的更多相关文章
- [Swift]LeetCode538. 把二叉搜索树转换为累加树 | 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 ...
- leetcode538. Convert BST to Greater Tree
https://www.cnblogs.com/grandyang/p/6591526.html 这个题本质上是中序遍历的反向.中序遍历是从小到大,而这个题目是从大到小,然后每个数加上比自己大的所有数 ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- Windows----Github环境搭建
关于Github的那点事: 一.下载安装 下载地址: https://git-for-windows.github.io/ 在官方下载完后,安装到Windows Explorer integra ...
- c++ 生成dll文件并调用-转
.h(头文件) .lib(库文件) .dll(动态链接库文件) 之间的关系和作用的区分 .h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的. 附加依赖项的是.lib不是.dll, ...
- python day20面向对象-属性,类方法,静态方法
一.属性的初识 # class Person: # # def __init__(self,name,hight,weight): # self.name = name # self.__hight ...
- 使用maven-shade-plugin打包spring项目为可执行的jar包
使用maven-shade-plugin打包spring项目为可执行的jar包,打包后的jar包里面包含依赖的jar包. POM文件: <plugin> <groupId>or ...
- 【leetcode】448. Find All Numbers Disappeared in an Array
problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...
- Go语言for循环语句
for循环语句解释与使用 首先我们定义sum:=0 格式:for i:=1;i<=100;i++{ sum+=i } for 循环100次,首先执行i=1,然后判断i<100,再执行sum ...
- 初学版本控制更新Version control
概述: 在学习计算机软件工程中,修订控制是跟踪和控制源代码更改的任何类型的实践. 对于软件开发人员有时会使用修订控制软件来维护文档和配置文件以及源代码. 当团队设计,开发和部署软件时,通常会将同一软件 ...
- 学习笔记TF038:实现估值网络
Q-Learning,学习Action对应期望值(Expected Utility).1989年,Watkins提出.收敛性,1992年,Watkins和Dayan共同证明.学习期望价值,从当前一步到 ...
- Java面向对象 第6节 异常
一.认识异常 异常是指在程序运行过程中所发生的不正常事件,如文件找不到.网络连接不通或链接中断.算数运算出错.数组下标越界.装在一个不存在的类.对null对象操作.类型转换异常等.异常会中断正在运行的 ...
- ionic3 热更新发布步骤记录
1.安装基本框架npm install -g ionic@latest npm install -g cordova ionic 验证版本号 ionic –version cordova -versi ...