当前位置:网站首页>LeetCode 897. Searching Trees in Ascending Order
LeetCode 897. Searching Trees in Ascending Order
2022-08-06 12:02:00 【JIeJaitt】
给你一棵二叉搜索树的 root ,请你 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没有左子节点,只有一个右子节点.
示例 1:

输入:root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
输出:
[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
示例 2:

输入:root = [5,1,7]
输出:[1,null,5,null,7]
提示:
- 树中节点数的取值范围是
[1, 100] 0 <= Node.val <= 1000
中序遍历之后生成新的树,题目要求我们返回按照中序遍历的结果改造而成的、only the right node等价二叉搜索树.我们可以进行如下操作:
先对输入的二叉搜索树执行中序遍历,将结果保存到一个列表中;
然后根据列表中的节点值,创建等价的只含有右节点的二叉搜索树,其过程等价于根据节点值创建一个链表.
- 时间复杂度: O ( n ) O(n) O(n),其中 n n n is the total number of nodes in the binary search tree.
- 空间复杂度: O ( n ) O(n) O(n),其中 n n n is the total number of nodes in the binary search tree.需要长度为 n n n The list holds the values of all nodes of the binary search tree.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
void inorder(TreeNode *node,vector<int> &res) {
if(node==nullptr) return;
inorder(node->left,res);
res.push_back(node->val);
inorder(node->right,res);
}
TreeNode* increasingBST(TreeNode* root) {
vector<int> res;
inorder(root,res);
TreeNode *dummyNode = new TreeNode(-1);
TreeNode *currNode = dummyNode;
for(int value: res) {
currNode->right = new TreeNode(value);
currNode = currNode->right;
}
return dummyNode->right;
}
};
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func increasingBST(root *TreeNode) *TreeNode {
vals := []int{
}
var inorder func(*TreeNode)
inorder = func(node *TreeNode) {
if node != nil {
inorder(node.Left)
vals = append(vals, node.Val)
inorder(node.Right)
}
}
inorder(root)
dummyNode := &TreeNode{
}
curNode := dummyNode
for _, val := range vals {
curNode.Right = &TreeNode{
Val: val}
curNode = curNode.Right
}
return dummyNode.Right
}
在中序遍历的过程中改变节点指向.Method 1 needs to traverse the binary search tree once,Then create a new equivalent binary search tree.事实上,It is also possible to traverse the input binary search tree once,In the process of traversing, change the node point to meet the requirements of the problem.
during in-order traversal,It can be achieved by modifying the node pointing.具体地,当我们遍历到一个节点时,把它的左孩子设为空,并将其本身作为上一个遍历到的节点的右孩子.Some imagination is required here.During recursive traversal,Since the call stack of the recursive function holds the reference to the node,Therefore, the above operation can be realized.The slides below demonstrate such a process.
- 时间复杂度: O ( n ) O(n) O(n),其中 n n n is the total number of nodes in the binary search tree.
- 空间复杂度: O ( n ) O(n) O(n).The stack space overhead in the recursive process is O ( n ) O(n) O(n).
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
// 尾节点
TreeNode* tail;
void dfs(TreeNode* root) {
if (!root) return ;
dfs(root->left);
// (1)The next node of the tail node becomes the current node
// (2)Then turn the tail node into a new node
tail = tail->right = root;
// 清空左子树
root->left = nullptr;
dfs(root->right);
}
TreeNode* increasingBST(TreeNode* root) {
// 创建一个虚拟头节点
auto dummy = new TreeNode();
tail = dummy;
dfs(root);
return dummy->right;
}
};
class Solution {
private:
TreeNode *resNode;
public:
void inorder(TreeNode *node) {
if (node == nullptr) {
return;
}
inorder(node->left);
// 在中序遍历的过程中修改节点指向
resNode->right = node;
node->left = nullptr;
resNode = node;
inorder(node->right);
}
TreeNode *increasingBST(TreeNode *root) {
TreeNode *dummyNode = new TreeNode(-1);
resNode = dummyNode;
inorder(root);
return dummyNode->right;
}
};
func increasingBST(root *TreeNode) *TreeNode {
dummyNode := &TreeNode{
}
resNode := dummyNode
var inorder func(*TreeNode)
inorder = func(node *TreeNode) {
if node == nil {
return
}
inorder(node.Left)
// 在中序遍历的过程中修改节点指向
resNode.Right = node
node.Left = nil
resNode = node
inorder(node.Right)
}
inorder(root)
return dummyNode.Right
}
边栏推荐
- 腾讯35k出来的,他让我见识到什么叫“精通MySQL调优”
- 1408. 数组中的字符串匹配 : 简单模拟题
- PS6603-USB PD 协议 SINK 端输出控制器芯片
- 【云原生 · Kubernetes】Kubernetes容器云平台部署与运维
- XML使用
- PS6603 代理直销Type-C PD 电源传输接收 SINK 端控制器芯片
- A domestic placeholder service
- 一文搞懂什么是kubernetes Service
- locust做性能测试
- Su Qiugui: The Secret of Google's Subdivision Deployment of Foreign Trade Enterprises
猜你喜欢

【SSL集训DAY1】B【动态规划】

不安装运行时运行 .NET 程序

教你画像素画每周分享195期

结合实战,浅析GB/T28181(四)——录像回放及控制

机器学习实战-梯度下降法在线性回归模型中的使用

Microsoft's new service allows businesses to expand access to their threat intelligence repository
![[SQL brush questions] Day2----SQL syntax basic query](/img/6b/d3eb1fe4055b9919be83d43e3e5af7.png)
[SQL brush questions] Day2----SQL syntax basic query

Golang gin 配置腾讯云cos实现单文件与多文件上传

Kubernetes stain and tolerance

题目分析1
随机推荐
NC3 链表中环的入口结点
PG core technology articles--system fields in the table
MySQL数据库安装步骤(图文)
在常州“超级虚拟工厂”,中国智造正在“原力觉醒”
Disadvantages of Kubernetes Virtual Machine Deployment
kubernetes灰度发布
Introduction to SQLNET.ALLOWED_LOGON_VERSION parameter in SQLNET.ORA
教你画像素画每周分享195期
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
NC2 重排链表
Kubernetes 部署策略
嵌入式编程和PC编程的区别
Structure of the actual combat battalion module nine operations
Link Aggregation Brief
NC2 rearrangement linked list
NITZ time zone update air interface message
机器学习入门实战-KNN完成鸢尾花分类预测
STM32 startup process - startup_xxxx.s file analysis (MDK and GCC dual environment)
Guitar Pro8 Guitar software update log is introduced
NC1 Addition of Large Numbers