[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 of the tree, and every node has no left child and only 1 right child.
```
Example 1:
Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]
5
/ \
3 6
/ \
2 4 8
/ /
1 7 9
Output: [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
Note:
1. The number of nodes in the given tree will be between 1 and 100.
2. Each node will have a unique integer value from 0 to 1000.
<br>
这道题给了一棵二叉树,让我们对其进行重排序,使得最左结点变为根结点,而且整个树不能有左子结点,如题目中的例子所示,排序后的结果是一条向右下方延伸的直线。如果我们仔细观察题目中的例子,可以发现遍历顺序其实是 左->根->右,就是中序遍历的顺序,虽然题目中没说是二叉搜索树,但这并不影响我们进行中序遍历。我们先从最简单的例子开始分析,当 root 为空时,直接返回空,当 root 没有左右子结点时,也是直接返回 root。当 root 只有一个左子结点时,我们此时要把其左子结点变为根结点,将原来的根结点变成其原来的左子结点的右子结点。但是如果 root 只有一个右子结点,还是保持原来的顺序不变,而若 root 同时具有左右子结点的话,还是要将左子结点变为根结点,然后把之前的根结点连到右子结点上,之前的右子结点还连在之前的根结点上,这个不用改变。我们可以发现,最麻烦的就是左子结点了,需要和其根结点交换位置,所以对于每个结点,我们需要知道其父结点的位置,那么就在递归函数的参数中传入一个 pre 结点,再对左右子结点调用递归函数时,都将其下一个要连接的结点传入,这个 pre 结点可能是当前结点或者当前结点的父结点。
在递归函数中,首先判空,若当前结点为空的话,直接返回 pre 结点,因为到空结点的时候,说明已经遍历到叶结点的下方了,那么 pre 就是这个叶结点了。由于是中序遍历,所以要先对左子结点调用递归函数,将返回值保存到一个新的结点 res 中,表示的意义是此时 node 的左子树已经全部捋直了,而且根结点就是 res,而且 node 结点本身也被连到了捋直后的左子树下,即此时左子结点和根结点已经完成了交换位子,当然要断开原来的连接,所以将 node->left 赋值为 nullptr。然后再对 node 的右子结点调用递归函数,注意此时的 pre 不能传入 node 本身,而是要传 node 结点的 pre 结点,这是因为右子结点后面要连接的是 node 的父结点,比如兑入下面这例子:
4
/
2
/
1 3
当运行到结点3的时候,pre 应该带入的是结点4,这样就可以把结点4连到结点3的右下方,从而正确的捋直,参见代码如下:
<br>
解法一:
class Solution {
public:
TreeNode* increasingBST(TreeNode* root) {
return helper(root, nullptr);
}
TreeNode* helper(TreeNode* node, TreeNode* pre) {
if (!node) return pre;
TreeNode* res = helper(node->left, node);
node->left = nullptr;
node->right = helper(node->right, pre);
return res;
}
};
<br>
我们也可以采用中序遍历的迭代形式,使用栈来辅助。由于根结点可能会产生变化,所以我们需要一个 dummy 结点,还需要一个 pre 结点。在 while 循环中,先找到最左结点,把路径上的所有结点都压入栈,然后取出栈顶元素,将其连到 pre 的右子结点上,并将 pre 更新为其右子结点,然后断开栈顶元素的左子结点连接,并将其移动到右子结点上,并继续循环,最终返回 dummy 的右子结点即可,参见代码如下:
<br>
解法二:
class Solution {
public:
TreeNode* increasingBST(TreeNode* root) {
TreeNode *dummy = new TreeNode(-1), pre = dummy;
stack<TreeNode> st;
while (root || !st.empty()) {
while (root) {
st.push(root);
root = root->left;
}
root = st.top(); st.pop();
pre->right = root;
pre = pre->right;
root->left = nullptr;
root = root->right;
}
return dummy->right;
}
};
<br>
Github 同步地址:
<https://github.com/grandyang/leetcode/issues/897>
<br>
参考资料:
<https://leetcode.com/problems/increasing-order-search-tree/>
<https://leetcode.com/problems/increasing-order-search-tree/discuss/251290/C%2B%2B-short-iterative>
<https://leetcode.com/problems/increasing-order-search-tree/discuss/165885/C%2B%2BJavaPython-Self-Explained-5-line-O(N)>
<br>
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 897. Increasing Order Search Tree 递增顺序查找树的更多相关文章
- 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_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 解题报告(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 ...
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
- PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
随机推荐
- macbook pro更换键盘的排线
以下是拆机图解 很简单的 有点动手能力的都可以更换 另外附加淘宝上的链接 https://item.taobao.com/item.htm?spm=a1z09.2.0.0.68a12e8dYAiQIx ...
- mybatis报错:Invalid bound statement (not found)
mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...
- mysql count的理解
mysql count的理解 1 select count(tel) as telcount from info;如果tel列有null 将不会被统计进去 2 count(*) 这样写性能更好 3 M ...
- 文件安全复制之 FastCopy
FastCopy是Windows平台上最快的文件拷贝.删除软件.由于其功能强劲,性能优越,一时间便超越相同类型的所有其他软件.由于该软件十分小巧,你甚至可以在安装后,直接将安装目录中的文件复制到任何可 ...
- 解决Chrome插件安装时程序包无效【CRX_HEADER_INVALID】的错误
将[.crx]后缀的文件拖拽至谷歌浏览器开发者模式下的扩展程序管理页签时,报错[CRX_HEADER_INVALID],即此插件无效的错误. 安装失败的原因 原因在于谷歌浏览器在新版本中添加对第三方插 ...
- 解决MybatisPlus修改时空字段不修改问题
今天遇到了一个问题,在更新数据时,MybatisPlus不会进行修改属性为空的数据表字段. 解决办法: 只需要在实体类的属性上加一行注释即可 /** * 姓名 */ @TableField(fill ...
- skeleton在心意web上的实践
通过手动编写skeleton,在fetch数据时显示skeleton loading,数据拉取成功隐藏skeleton 先看下效果图 在component下创建页面对应的skeleton,然后通过在i ...
- 基于直接缓冲区和非直接缓冲区的javaIO文件操作
基本概念: 1. 非直接缓冲区: 指的是通过jvm来缓存数据的,应用程序要读取本地数据要经历从本地磁盘到物理内存,然后copy到jvm中,然后再通过流的方式读取到应用程序中,写的操作正好与之相反. ...
- 《深入理解Java虚拟机》并发(第12~13章)笔记
volatile关键字的作用 所有变量的可见性--仅仅是修改后的值的可见性,不保证并发修改时新值和预期一致.即只保证读,不保证写. 禁止指令重排序--修饰的变量,读写不会指令重排.如变量isReady ...
- mp4文件格式解析二
目前MP4的概念被炒得很火,也很乱.最开始MP4指的是音频(MP3的升级版),即MPEG-2 AAC标准.随后MP4概念被转移到视频上,对应的是MPEG-4标准.而现在我们流行的叫法,多半是指能播放M ...