这是悦乐书的第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. mysql 5.6 bug

    https://dev.mysql.com/doc/refman/5.6/en/account-management-sql.html USE mysql; SELECT Host,User FROM ...

  2. Mac下通过命令行安装npm install -g 报错,如何解决?

    1, 使用 sudo npm install -g n2, 或者 sudo chmod -R 777 /usr/local/lib,然后 npm install -g

  3. 最大流EK算法

    给定一个有向图G=(V,E),把图中的边看作 管道,每条边上有一个权值,表示该管道 的流量上限.给定源点s和汇点t,现在假设 在s处有一个水源,t处有一个蓄水池,问从 s到t的最大水流量是多少? 网络 ...

  4. LightOJ1245 Harmonic Number (II) —— 规律

    题目链接:https://vjudge.net/problem/LightOJ-1245 1245 - Harmonic Number (II)    PDF (English) Statistics ...

  5. HDU5968 异或密码 —— 二分 + 边界的细节处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5968 异或密码 Time Limit: 2000/1000 MS (Java/Others)    M ...

  6. 关于Linux启动文件rc.local的解惑

    背景 首先,rc.local是Linux启动程序在login程序前执行的最后一个脚本,有的服务器中在rc.local中可能会有一句touch /var/lock/subsys/local,这是干什么的 ...

  7. U盘安装Ubuntu 14.04 LTS正式版

    Ubuntu 14.04 LTS正式版发布,而且提供五年的支持和维护服务.Ubuntu 14.04是Ubuntu开发团队历经五年的心血之作.许多新手都喜欢把Linux安装文件刻录成光盘再安装,而安装好 ...

  8. html5--5-11 绘制文字

    html5--5-11 绘制文字 学习要点 掌握文字的绘制方法 文字的绘制方法 strokeText("文字",x,y,maxWith) 绘制(描边)空心文字 fillText(& ...

  9. Android Studio中的“favorites”和“bookmark”

    做项目难免来回查看某个文件的某个方法,某些文件可能访问率很高, 为了加快开发效率楼主推荐使用favorites (文件)bookmark (代码 行). favorites 的添加就在文件单击右键ad ...

  10. web安全字体

    webfont解剖 Unicode字体可以包含数以千计字形 有四个字体格式: WOFF2, WOFF, EOT, TTF 一些字体格式需要使用GZIP压缩 一个web字体是字形的集合,且每个字形是一个 ...