LeetCode.897-递增搜索树(Increasing Order Search Tree)
这是悦乐书的第346次更新,第370篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第211题(顺位题号是897)。给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节点现在是树的根,并且每个节点都没有左子节点,只有一个右子节点。例如:
输入:[5,3,6,2,4,null,8,1,null,null,null,7,9]
5
/ \
3 6
/ \ \
2 4 8
/ / \
1 7 9
输出:[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
1
\
2
\
3
\
4
\
5
\
6
\
7
\
8
\
9
注意:
给定树中的节点数将介于1和100之间。
每个节点都有一个0到1000的唯一整数值。
02 第一种解法
先对原二叉树通过递归的方式进行中序遍历,将所有的节点值存入一个List中,以List中的第一个元素作为根节点,再遍历List中剩下的其他元素,作为树的右子节点,最后返回新树。
public TreeNode increasingBST(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
inorder(root, list);
TreeNode result = new TreeNode(list.get(0));
TreeNode ans = result;
for (int i=1; i<list.size(); i++) {
ans.right = new TreeNode(list.get(i));
ans = ans.right;
}
return result;
}
public void inorder(TreeNode node, List<Integer> list){
if (node == null) {
return ;
}
inorder(node.left, list);
list.add(node.val);
inorder(node.right, list);
}
03 第二种解法
思路和第一种解法一样,只是将中序遍历二叉树从递归换成了迭代。
public TreeNode increasingBST2(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
if (!stack.isEmpty()) {
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
TreeNode result = new TreeNode(list.get(0));
TreeNode ans = result;
for (int i=1; i<list.size(); i++) {
ans.right = new TreeNode(list.get(i));
ans = ans.right;
}
return result;
}
04 第三种解法
我们还可以再简化下,不使用List来存储原二叉树的节点值,直接将得到的节点值作为新二叉树的节点值即可。
public TreeNode increasingBST3(TreeNode root) {
TreeNode result = new TreeNode(0);
TreeNode ans = result;
Stack<TreeNode> stack = new Stack<TreeNode>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
if (!stack.isEmpty()) {
root = stack.pop();
// 直接处理节点,作为新树的右子节点
ans.right = new TreeNode(root.val);
ans = ans.right;
root = root.right;
}
}
return result.right;
}
05 第四种解法
针对上面的第三种解法,我们也可以使用递归来解。
TreeNode ans;
public TreeNode increasingBST4(TreeNode root) {
TreeNode result = new TreeNode(0);
ans = result;
helper(root);
return result.right;
}
public void helper(TreeNode root) {
if (root == null) {
return ;
}
helper(root.left);
ans.right = new TreeNode(root.val);
ans = ans.right;
helper(root.right);
}
06 小结
算法专题目前已连续日更超过六个月,算法题文章214+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.897-递增搜索树(Increasing Order Search Tree)的更多相关文章
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
- 【Leetcode_easy】897. Increasing Order Search Tree
problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完
- 897. Increasing Order Search Tree
题目来源: https://leetcode.com/problems/increasing-order-search-tree/ 自我感觉难度/真实难度:medium/easy 题意: 分析: 自己 ...
- [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- LeetCode 897 Increasing Order Search Tree 解题报告
题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...
- 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
- [LeetCode&Python] Problem 897. Increasing Order Search Tree
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- 【leetcode】897. Increasing Order Search Tree
题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...
- [Swift]LeetCode897. 递增顺序查找树 | Increasing Order Search Tree
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
随机推荐
- maven的基本原理和使用
一.Maven中央存储库 当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载.首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源,如 ...
- 修改JDK环境变量,不生效的问题
一般是在/etc/profile下面配置JDK的环境变量 JAVA_HOME=/data/jdk1.7.0_72 JRE_HOME=/data/jdk1.7.0_72/jre PATH=$PATH:$ ...
- linux apache 用户认证:
root@ubuntu:/# htpasswd -c /etc/apache2/password zhangsan (-c表示要创建一个password密码文件,文件存放目录是/etc/apache2 ...
- Spring AOP-xml配置
在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...
- [CPP - STL] swap技巧
最近在看<Effective STL>,[条款17:使用“交换技巧”修整过剩容量]中提到容器的成函数void swap(container& from),即实现容器对象与from对 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- cocoapod使用
什么是cocoapod CocoaPods是用于方便使用第三方开源库的管理工具,减少我们对第三方库的各种配置. 安装教程参考: CocoaPods的介绍.安装.使用和原理 Cocoapod安装使用 第 ...
- 自建 AppRTC
自建 AppRTC 字数3158 阅读1718 评论2 喜欢2 AppRTC 是 webrtc 的一个 demo.自建 AppRTC 可以苦其心志劳其筋骨饿其体肤,更重要的是能学会 webrtc 服务 ...
- 关于使用response.addHeader下载中文名乱码问题
介绍下我项目中遇到的问题:在数据库导出Excel文件的过程中,导出文件中文名始终异常,最终结果发现需要在response.addHeader 中的 filename = "xxxx" ...
- hdu-5742 It's All In The Mind(数学)
题目链接: It's All In The Mind Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...