[LeetCode] 538. 把二叉搜索树转换为累加树 ☆(中序遍历变形)
把二叉搜索树转换为累加树
描述
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。
例如:
输入: 二叉搜索树:
5
/ \
2 13
输出: 转换为累加树:
18
/ \
20 13
解析
标准中序遍历,再反着遍历,每个节点的值 += 前一个节点的值。
代码
傻方法
先把树,左右全部交换,再标准中序遍历,再左右交换回来。
public TreeNode convertBST(TreeNode root) {
if (null == root) {
return null;
}
swap(root);
TreeNode temp = root;
int preVal = 0;
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || null != temp) {
if (null != temp) {
stack.push(temp);
temp = temp.left;
} else {
TreeNode curNode = stack.pop();
curNode.val += preVal;
preVal = curNode.val;
temp = curNode.right;
}
}
return root;
}
public void swap(TreeNode root) {
if (null == root) {
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
swap(root.left);
swap(root.right);
}
中序遍历变形--栈迭代
public TreeNode convertBST(TreeNode root) {
if (null == root) {
return null;
}
TreeNode temp = root;
int preVal = 0;
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || null != temp) {
if (null != temp) {
stack.push(temp);
temp = temp.right;
} else {
TreeNode curNode = stack.pop();
curNode.val += preVal;
preVal = curNode.val;
temp = curNode.left;
}
}
return root;
}
中序遍历变形--递归
private int sum = 0;
public TreeNode convertBST(TreeNode root) {
if (root != null) {
convertBST(root.right);
sum += root.val;
root.val = sum;
convertBST(root.left);
}
return root;
}
[LeetCode] 538. 把二叉搜索树转换为累加树 ☆(中序遍历变形)的更多相关文章
- Java实现 LeetCode 538 把二叉搜索树转换为累加树(遍历树)
538. 把二叉搜索树转换为累加树 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和 ...
- Leetcode 538. 把二叉搜索树转换为累加树
题目链接 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 大于它的节点值之和. 例如: 输入: ...
- LeetCode 把二叉搜索树转换为累加树
第538题 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉 ...
- 538 Convert BST to Greater Tree 把二叉搜索树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树: ...
- [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 ...
- [LC]783题 二叉搜索树结点最小距离(中序遍历)
①题目 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null]输出: 1解释:注意,root是树结点对象(T ...
- 【LeetCode】二叉搜索树的前序,中序,后续遍历非递归方法
前序遍历 public List<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> list = n ...
- LeetCode 98. 验证二叉搜索树 | Python
98. 验证二叉搜索树 题目来源:https://leetcode-cn.com/problems/validate-binary-search-tree 题目 给定一个二叉树,判断其是否是一个有效的 ...
- 剑指Offer25 二叉搜索树转换为排序双向链表
/************************************************************************* > File Name: 25_BSTCon ...
随机推荐
- 【转】Revit二次开发——读取cad中的文字信息
Revit读取cad的文字信息需要借助Teigha的开源dll,在程序中添加下图中红色框的dll文件的引用,其他的dll文件全部放在同一个文件夹中即可,运行的时候,会自动把这些dll文件全部复制到bi ...
- git clone 某个链接时候报错Initialized empty Git repository in 不能克隆
查看下是不是git是不是1.7.1版本. git --version 使用 yum -y update 更新一下. 再使用git clone 虽然还是会提示这个报错,但是可以克隆了.亲测有效. git ...
- [Linux]Linux下samba创建共享文件
1. 安装samba服务 yum install -y samba 2. 创建需要共享的目录 在目录/home/xxxx/share xxx为用户名 mkdir share 修改该目录权限(上层文件夹 ...
- C#中的ref和out与SQL中的output
什么时候会需要使用ref和out 有时,我们会需要获取某个值在方法中的运行状态,根据定义的方法,我们仅仅能够获得一个返回值,但是,有时我们也许想获取多个值,通过返回值就不能返回这样的信息,我们可以通过 ...
- [xsy3553]游戏
题意:交互题,交互库有长为$n$的$01$串$S$,你可以用字符串$T$询问$\sum\limits_{i=1}^n[S_i=T_i]$,要求用$1030$次询问问出$S$,$n=5000$ 首先我们 ...
- readiness与liveness
一.liveness(存活探针)方式 HTTP GET:对指定的端口和路径执行http get请求,返回非错误代码即代表正常 TCP socket:对指定端口建立TCP链接,链接通过则代表正常 Exe ...
- tomcat启动时报No rules found matching 'Server/Service/Engine/Host/context'
tomcat是8.0版本. 在eclipse启动时,第二行报这个, 同时项目也没加载(tomcat启动成功了). 网上搜了半天, 试了半天, 没搞定. 最后不经意间发现: <Context do ...
- QT 学习基础问题记录
1. connect 函数 需要先创建发送者和接收者实例,并且信号函数和槽函数如果有参数,需要在 connect 函数使用时指定相关参数类型. 2.窗口控件设置 设置窗口的最大化.最小化.问号提示等控 ...
- day55——django引入、小型django(socket包装的服务器)
day55 吴超老师Django总网页:https://www.cnblogs.com/clschao/articles/10526431.html 请求(网址访问,提交数据等等) request 响 ...
- 选择类排序 (简单选择排序,堆排序)— c语言实现
选择类排序包括: (1) 简单选择排序 (2)树形选择排序 (3)堆排序 简单选择排序: [算法思想]:在第 i 趟简单选择排序中,从第 i 个记录开始,通过 n - i 次关键字比较,从 n - ...