99. Recover Binary Search Tree (Tree; DFS)
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
class Solution {
public:
void recoverTree(TreeNode *root) {
vals.clear();
treeNodes.clear();
inorderTraverse(root);
sort(vals.begin(), vals.end());
for (int i = ; i < treeNodes.size(); ++i)
{
treeNodes[i]->val = vals[i]; //只改变值,不改结构
}
}
void inorderTraverse(TreeNode* root)
{
if (!root)
return;
inorderTraverse(root->left);
vals.push_back(root->val);
treeNodes.push_back(root);
inorderTraverse(root->right);
}
private:
vector<int> vals;
vector<TreeNode*> treeNodes;
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void recoverTree(TreeNode* root) {
TreeNode* pre = NULL;
TreeNode* swap1 = NULL;
TreeNode* swap2 = NULL;
inOrderTraverse(root,pre,swap1,swap2); int tmp = swap1->val;
swap1->val = swap2->val;
swap2->val = tmp;
} void inOrderTraverse(TreeNode* root, TreeNode* &pre,TreeNode* &swap1,TreeNode* &swap2){ //important to use &, otherwise new object will use a new address and the result won't bring back to caller //visit left child
if(root->left) inOrderTraverse(root->left,pre,swap1,swap2); //visit root
if(pre && root->val < pre->val){
swap2 = root;
if(swap1==NULL) {
swap1 = pre;
}
}
pre = root; //visit right child
if(root->right) inOrderTraverse(root->right,pre,swap1,swap2);
}
};
99. Recover Binary Search Tree (Tree; DFS)的更多相关文章
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 99. Recover Binary Search Tree
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- LeetCode OJ 99. Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- elixir jenkins 集成构建方式配置
备注: 主要问题是环境变量配置的问题,解决方法是使用软连接进行解决 1. 下载软件包 wget https://github.com/elixir-lang/elixir/releases/ ...
- VMware harbor && minio 搭建企业docker私有镜像以及需要注意的问题
1. docker harbor 配置 建议使用在线安装的模式(离线包太大了) 首先需要安装docker-compose .docker .mino (具体安装可以参考官网后者我的博 ...
- java之反射概述
类加载器和反射 类加载器: 1 类的加载过程: 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化三步骤来实现对这个类进行初始化. 加载:就是指将class文件读入内存 ...
- 构建docker私有库
前提: ip: 172.16.0.9 docker: Version: 18.05.0-ce 1下载registry docker pull registry 2 建库 将库像 ...
- MDK警告 warning: #111-D: statement is unreachable
解析: "statement is unreachable"这句一般是说编译器认为程序执行不到这里. 因为本人运行程序的时候,再向前有一个While(1)循环, 理论上说除非你里面 ...
- Sentinel-dashboard
Dashboard控制台 sentinel-dashboard是一个单独的应用,通过spring-boot进行启动,主要提供一个轻量级的控制台,它提供机器发现.单机资源实时监控.集群资源汇总,以及规则 ...
- Mac OS Virtualbox 倒入 ova 镜像文件
原文:https://www.zhihu.com/question/40785995 ------------------ 自己亲测 --------------------- 把 windows 系 ...
- JDK之集合乱序源码分析
在JAVA的JDK中Collections类提供了shuffle方法用来对给定的集合参数进行乱序重排,之前面试也被问到过类似的问题,看了一下JDK的源码实现做个记录 1. 方法签名: Collecti ...
- 【学步者日记】UnityEditor扩展菜单以及ScriptableObject
完整版链接:http://note.youdao.com/noteshare?id=c54f35ca19371886e6a94302387bb6cd 下面是预览的部分,带图的版本请看上面链接. ...
- Sql--------服务器的数据库表数据插入到本地数据库
本地语句:::insert into 表名(列名) SELECT * FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=127.0.0.1;User ID=sa ...