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 ...
随机推荐
- 生产环境LNMP (果图片)
一. 下载一键安装包 LNMP 官方地址为:http://lnmp.org/ 登陆后运行:screen -S lnmp cd /usr/local/src wget -c http://soft. ...
- OpenCL入门
初入OpenCL,做个记录. 在Windows下开发OpenCL程序,必须先下载OpenCL的SDK,现在AMD,NVIDIA,Intel均提供各自的OpenCL库,基本是大同小异.安装好SDK后新建 ...
- Eclipse导入工程后,XDoclet错误:Missing library: xdoclet-1.2.1.jar. Select the home directory for XDoclet
这几天在使用Open Health Tools的OpenXDS工程,在导入Eclipse后,出现下面的错误: 遂google之,在网上找到了答案.答案网址为http://blog.v-s-f.co.u ...
- 从内存的角度观察 堆、栈、全局区(静态区)(static)、文字常量区、程序代码区
之前写了一篇堆栈的,这里再补充下内存其他的区域 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. 2.堆区(heap) — 一般由程 ...
- vs2012加载T4MVC模板
(1)在工程项目上右键点击“管理NuGet程序包”,在线搜索T4MVC模板,选择并安装,安装成功后,项目中会添加T4MVC.tt文件及子文件. (2)如果添加了新的控制器,则右击T4MVC.tt文件点 ...
- qt在动态库里面加载widget的例子
testDll和testExe项目 备注:windows下dll内不需要new QApplication, linux和mac下面需要在动态库里面new QApplication testdll.h ...
- [Web]网址净化方法
本文来自:https://meta.appinn.com/t/topic/3130 原理很简单,所以不说了. 用法很简单,先把下面的代码保存为书签(复制到地址里面),在需要的页面里点击一下这个书签就好 ...
- 定位SDK返回时间问题
关于怎么使用定位SDK ,这里不做介绍,可以去看api:http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v5-0 ...
- NGUI中LabelA停靠LabelB的方法
详情看 http://note.youdao.com/noteshare?id=ec901d56341207052b2d19233b5ddba3 这里仅仅贴出文字,完整内容请看上面链接. 有这样一个需 ...
- 【学习笔记】Manacher
扔板子跑路 代码 POJ3974 #include <cstdio> #include <cstring> #include <algorithm> using n ...