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 ...
随机推荐
- Portainer docker 可视化管理工具
1. 快速使用 docker run -d -p 9000:9000 portainer/portainer 2. docker swarm 模式 docker service create \ ...
- hashids 了解
用于隐藏真实的id 原理是从数字经过一个加盐(salted)算法产生一个哈希(hash)字符串.这样算法就是通过混淆使结果具有不可预测性,而唯一性依然由数字本身来达成,从而得到(类似 youtube ...
- centos65安装docker遇到的问题
1.安装docker后启动显示内核太低(升级内核): 网上太多方案 2.升级内核后还是启动不了docker:执行下面语句 yum install device-mapper-event-libs 步骤 ...
- Centos6.8 安装MySql
启动Centos6.8 输入命令: yum install mysql mysql-server -y 等待安装完成. 启动MySQL,输入命令: /etc/init.d/mysqld s ...
- C++代码规范之命名
C++代码规范之命名 一.命名的两个基本原则 1.含义清晰,不易混淆: 2.不和其它模块.系统API的命名空间相冲突. 二.命名通则 1.在所有命名中,都应使用标准的英文单词或缩写:不得使用拼音或拼音 ...
- GITBOOK/HEXO TRAVIS GITHUB-PAGES 博客搭建
简介 这年头要是没有个博客都不好意思给别人说你是程序员,我用XX笔记呀,不行吗?不行,这玩意儿要么不能公开分享,要么公开分享要会员,现在到处都是开源,自己学到了东西都不能分享给需要帮助的人,真是伤心呀 ...
- python 源代码保护 之 xx.py -> xx.so
前情提要 之前由于项目的需要,需要我们将一部分“关键代码”隐藏起来. 虽然Python 先天支持 将源代码 编译后 生成 xxx.pyc 文件,但是破解起来相当容易 -_-!! 于是搜罗到了另外一种方 ...
- 启动 Eclipse 报错 “An internal error occurred during: "Initializing Java Tooling". java.lang.NullPointerException”
之前在线升级了Eclipse,由于网络/或者是设置问题,在升级完成后启动Eclipse出线上述错误... 解决方法 1. 删除目录工作目录下面的.project文件夹: 如下图: 2. 关闭Eclip ...
- PHP for和foreach的区别
首先,我们先准备两个用于遍历的数组: $arr1=array(1=>'a', 3=>22, 5=>'b', 4=>'c', 8=>'d'); $arr2=array('a ...
- [POJ] Bode Plot
Description Consider the AC circuit below. We will assume that the circuit is in steady-state. Thus, ...