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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- 手把手教你用git
一.如何安装git 下载地址: https://git-scm.com/download/win 根据自己的电脑选择是32位的还是64位的.下载完后直接运行,之后一直next就好了.安装成功后,会有这 ...
- XLua热更新用法全流程总结(所有容易出问题的点)
Xlua热更新流程总结 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创 ...
- Docker跨主机link
user case:一个app container向一个oracle container跨主机传输数据. 思路一:将oracle对外暴露端口,将hostA的IP添加入app上/ect/hosts上.这 ...
- Win下更新pip出现OSError:[WinError17]与PerrmissionError:[WinError5]及解决
环境:Win7 64位,python3.6.0 我在准备用pip装东西的时候,在cmd里先更新了一下pip,大概是9.0.1更新到9.0. 尝试更新pip命令: pip install --upgra ...
- LeetCode - Online Election
In an election, the i-th vote was cast for persons[i] at time times[i]. Now, we would like to implem ...
- 【java多线程】队列系统之LinkedBlockingDeque源码
1.简介 上一篇我们介绍了 LinkedBlockingDeque 的兄弟篇 LinkedBlockingQueue .听名字也知道一个实现了 Queue 接口,一个实现了 Deque 接口,由于 D ...
- centos7忘记密码处理办法
centos7忘记密码处理办法 此界面按e进入grub编辑界面 进入grub编辑界面.把linux16这行的ro修改为rw init=/sysroot/bin/sh. 按ctrl+x进入单用户模式 登 ...
- easyUI默认图标的使用
使用格式如下: <table id="table" class="easyui-datagrid" style="width:600px;hei ...
- HBase各版本对Hadoop版本的支持情况
转载自:http://blog.csdn.net/sunny05296/article/details/54089194 安装HBase时,要考虑选择正确的Hadoop版本,否则可能出现不兼容的情况. ...
- Android 动态注册JNI函数
1.JNI函数注册方式 在Android开发中,由于种种原因我们需要调用C/C++代码,在这个时候我们就需要使用jni了, jni在使用时要对定义的函数进行注册,这样java才能通过native关键字 ...