当前位置:网站首页>LeetCode 99. Restore binary search tree -- medium order traversal + sorting
LeetCode 99. Restore binary search tree -- medium order traversal + sorting
2022-04-22 05:53:00 【Guapifang】
- Restore binary search tree
Give you the root node of the binary search tree root , In the tree just The values of the two nodes are incorrectly exchanged . Please... Without changing its structure , Restore the tree .
Example 1:

Input :root = [1,3,null,null,2]
Output :[3,1,null,null,2]
explain :3 It can't be 1 The left child , because 3 > 1 . In exchange for 1 and 3 Make the binary search tree efficient .
Example 2:

Input :root = [3,1,4,null,null,2]
Output :[2,1,4,null,null,3]
explain :2 Can't be in 3 In the right subtree , because 2 < 3 . In exchange for 2 and 3 Make the binary search tree efficient .
Tips :
The number of nodes on the tree is in the range [2, 1000] Inside
-231 <= Node.val <= 231 - 1
Answer key
Arrange an order and traverse the middle order .
AC Code
/** * 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:
vector<int>q;
void dfs(TreeNode* root)
{
if(root==NULL)return;
q.push_back(root->val);
dfs(root->left);
dfs(root->right);
}
int node_id;
void dfs2(TreeNode* root)// In the sequence traversal
{
if(root==NULL)return ;
dfs2(root->left);
root->val = q[node_id];
node_id ++;
dfs2(root->right);
}
void recoverTree(TreeNode* root)
{
dfs(root);
sort(q.begin(),q.end());
node_id = 0;
dfs2(root);
}
};

版权声明
本文为[Guapifang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220536184849.html
边栏推荐
- 数据挖掘——数据预处理
- LeetCode 514. 自由之路--动态规划
- Coordinate conversion using Gaode map API: WGS84 → gcj02
- Several ways to exchange two variable values without the third variable
- Uniapp: hbuilderx runs the uniapp project to the night God simulator
- Miniconda source swap (add image)
- Exploration des données - regroupement
- excel根据单元格内容设定行列颜色
- Program compilation (preprocessing operation) + link
- Root cause: the package import downloaded by the PIP terminal cannot be used
猜你喜欢

golang 把句中的每个单词的首字母变成大写(笔试题)

torch使用踩坑日记,矩阵加速运算

raspberry keras-ocr can‘t allocate memory in static TLS block

vs 断点无法调试 The breakpoint will not currently be hit. No symbols have been loaded for this document.

The breakpoint will not currently be hit No symbols have been loaded for this document.

scikit-learn中的PCA

蓝桥杯31天冲刺 Day16

excel根据单元格内容设定行列颜色

Force buckle 237 Delete specified node list

最长公共子序列(动态规划)
随机推荐
蓝桥杯31天冲刺 Day4
简单dp问题-母牛繁殖和超级爬楼梯
Pseudo code block writing (for paper writing)
Convolutional neural network
Usage of JSON type field in MySQL
蓝桥杯31天冲刺 Day8
golang 计算天数 四舍五入 time
二分类任务为什么常见用softmax而不是sigmoid
不用第三个变量交换两变量值的几种方式
整数拆分问题(动态规划+递归&记录数组)
raspberry keras-ocr can‘t allocate memory in static TLS block
Program compilation (preprocessing operation) + link
Data mining -- sequential pattern mining
蓝桥杯31天冲刺 Day14
【2022阿里安全】真实场景篡改图像检测挑战赛 决赛rank17方案分享
Why introduce collaborative process
数据挖掘——序列模式挖掘
Longest common subsequence (dynamic programming)
codeforces div2 777 A
vscode cmake-tools launching. JSON usage document (garbage translation)