这是悦乐书的第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)的更多相关文章

  1. LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)

    897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...

  2. 【Leetcode_easy】897. Increasing Order Search Tree

    problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完

  3. 897. Increasing Order Search Tree

    题目来源: https://leetcode.com/problems/increasing-order-search-tree/ 自我感觉难度/真实难度:medium/easy 题意: 分析: 自己 ...

  4. [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 ...

  5. 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 ...

  6. 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...

  7. [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 ...

  8. 【leetcode】897. Increasing Order Search Tree

    题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...

  9. [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 ...

随机推荐

  1. Thumbelina,摘自iOS应用Snow White and more stories

    Once upon a time there was a woman who wanted to have a child. 从前,有个想要个孩子的女人. A witch heard her wish ...

  2. [IR课程笔记]概率检索模型

    几个符号意义: R:相关文档集 NR:不相关文档集 q:用户查询 dj:文档j 1/0风险情况 PRP(probability ranking principle):概率排序原理,利用概率模型来估计每 ...

  3. 对私有API提交的注意事项

    1.这个等于堵死了调试断点.关闭就不能断点调试了. 2.对于敏感的函数名要做一个对称加密处理. 防止二进制文件的静态扫描. 3.对于调用私有函数的方法,可以做一个宏定义包装. #define 你的正常 ...

  4. jvm调试

    https://www.usenix.org/legacy/events/jvm01/full_papers/russell/russell_html/index.html

  5. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  6. Gym - 100187E E - Two Labyrinths —— bfs

    题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...

  7. Dubbo与Zookeeper、SpringMVC整合与使用(干货-理论放一遍。。。还未完结!)

    Dubbo跟Zookeeper的简介分享两个不错的链接: Dubbo简介:http://shiyanjun.cn/archives/325.html Zookeeper简介:http://www.op ...

  8. WPF Combo box 获取选择的Tag

    string str1 = ((ComboBoxItem)this.cboBoxRate1553B.Items[this.cboBoxRate1553B.SelectedIndex]).Tag.ToS ...

  9. 通过kettle数据导入mysql时,空值的处理在插入mysql时,会自动转转换为null值,无法插入

    1.windows下C:\Users\用户名\.kettle目录中找到kettle.properties文件,增加KETTLE_EMPTY_STRING_DIFFERS_FROM_NULL=Y2.Li ...

  10. .html 页面修改成 .jsp 后缀后中文乱码解决办法。

    .html 后缀的文件,如果直接将 .html后缀改成 .jsp 后缀,则会乱码. 正确方法如下: 将如图的代码中 html  声明去掉,然后加上这段代码:<%@ page language=& ...