这是悦乐书的第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. HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件

    HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件.它将每个文件存储成一系列的数据块,除了最后一个,所有的数据块都是同样大小的.为了容错,文件的所有数据块都会有副本.每个文件的数据块大小和副本 ...

  2. MongoDB 学习五:索引

    这章我们介绍MongoDB的索引,用来优化查询. 索引介绍 数据库索引有些类似书的目录. 一个查询如果没有使用索引被称为表扫描,意思是它必须像阅读整本书那样去获取一个查询结果.一般来说,我们应尽量避免 ...

  3. [haoi2014]贴海报

    Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙.张贴规则如下:1.electoral墙是 ...

  4. ffmpeg 中av_rescale_rnd 的含义

    http://blog.csdn.net/fireroll/article/details/8485482 一.函数声明: int64_t av_rescale_rnd(int64_t a, int6 ...

  5. innerText和innerHTML

    起因 由于公司的项目以前不考虑浏览器的兼容性问题,当时只考虑ie8浏览器,封装的控件也只针对ie8,我后面的做的时候,也就针对ie8,最近发现,封装的日期控件,在firefox竟然没法显示出来,去看J ...

  6. vertical-align 的理解

    1.vertical-align 属性和值列表

  7. 清理html中空白符/空格/换行在行内元素中产生的间距

    问题:行内元素之间产生间隔 原因:换行符,Tab制表符,空格产生间隔 解决方法: 1.行内元素写成一行 2.设置font-size为0px 把父级文本设置为0px; 再为需要显示文字的行内元素设置文字 ...

  8. 【旧文章搬运】为什么win32k.sys在System进程空间无法访问

    原文发表于百度空间,2010-01-02========================================================================== 玩过Sha ...

  9. CentOS 6.5远程连接工具x shell

    安装X shell 在Window系统下远程连接Linux,x shell只是一种远程连接工具,类似工具还有CRT.VNC.putty. 以下是安装X shell的注意事项 此选项中,如不把——初始数 ...

  10. Thief in a Shop

    题意: 问n个物品选出K个可以拼成的体积有哪些. 解法: 多项式裸题,注意到本题中 $A(x)^K$ 的系数会非常大,采用NTT优于FFT. NTT 采用两个 $2^t+1$ 质数,求原根 $g_n$ ...