39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree
OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/
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?
思想: Morris traversal.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// morris traversal
/******************************************************************************************/
/* Inorder Traversal(should get ascending seq.):Analysis:
case A: If 2 near nodes swapped,then there will be just 1 Inversion Pair.
case B: If 2 nodes not near swapped,then there will be 2 Inversion Pairs.
Weather case A or case B, swap the max-value and the min-value of the Inversion Pair(s).*/
/*****************************************************************************************/
class Solution {
public:
void recoverTree(TreeNode *root) {
TreeNode *cur, *pre, *node1, *node2; // node1, node2: Record 2 near nodes
TreeNode *first, *second; // Record 2 swapping nodes
node1 = node2 = first = NULL;
cur = root;
while(cur) {
if(cur->left == NULL) {
if(node1 == NULL) node1 = cur;
else if(node2 == NULL) node2 = cur;
else { node1 = node2; node2 = cur;}
cur = cur->right;
} else {
pre = cur->left;
while(pre->right && pre->right != cur) pre = pre->right;
if(pre->right == NULL) {
pre->right = cur;
cur = cur->left;
continue;
} else {
pre->right = NULL;
if(node2 == NULL) node2 = cur;
else {node1 = node2; node2 = cur;}
cur = cur->right;
}
}
if(node1 && node2 && node1->val > node2->val) {
if(first == NULL) first = node1;
second = node2;
}
}
// already learn that there exist 2 nodes swapped.
int t = first->val;
first->val = second->val;
second->val = t;
}
};
Validate Binary Search Tree
OJ: https://oj.leetcode.com/problems/validate-binary-search-tree/
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Thoughts: As I posted on the discuss forum of Leedcode.
Solution 1 : Preorder traversal
class Solution {
public:
bool isValidBST(TreeNode *root) {
if(root == NULL) return true;
TreeNode *pre = NULL, *post = NULL;
if(root->left) {
pre = root->left;
while(pre->right) pre = pre->right;
if(pre->val >= root->val) return false;
}
if(root->right) {
post = root->right;
while(post->left) post = post->left;
if(post->val <= root->val) return false;
}
return isValidBST(root->left) && isValidBST(root->right);
}
};
Solution 2: Inorder traversal.
bool isBST(TreeNode *root, int& preV) {
if(root == NULL) return true;
bool l = isBST(root->left, preV);
if(preV != INT_MIN && preV >= root->val) return false;
preV = root->val;
bool r = isBST(root->right, preV);
return l && r;
}
class Solution {
public:
bool isValidBST(TreeNode *root) {
int preV = INT_MIN; // There exists an Assert.
return isBST(root, preV);
}
};
Solution 3: Morris Traversal.
class Solution {
public:
void recoverTree(TreeNode *root) {
TreeNode *cur, *tem, *node1, *node2;
TreeNode *first, *second;
node1 = node2 = first = NULL;
cur = root;
while(cur) {
if(cur->left == NULL) {
if(node1 == NULL) node1 = cur;
else if(node2 == NULL) node2 = cur;
else { node1 = node2; node2 = cur;}
cur = cur->right;
} else {
tem = cur->left;
while(tem->right && tem->right != cur) tem = tem->right;
if(tem->right == NULL) {
tem->right = cur;
cur = cur->left;
continue;
} else {
tem->right = NULL;
if(node2 == NULL) node2 = cur;
else {node1 = node2; node2 = cur;}
cur = cur->right;
}
}
if(node1 && node2 && node1->val > node2->val) { if(first == NULL) first = node1;
second = node2;
}
}
int t = first->val;
first->val = second->val;
second->val = t;
}
};
39. Recover Binary Search Tree && Validate Binary Search Tree的更多相关文章
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- iMacros 教程
imacros能记录你在网页中的动作,然后模拟你的动作自动重复执行.进阶应用主要在于两个方面: 1.用JS动态调用,重复执行之. 2.调用CSV文件,这个不错哦. 还可以调用数据库,这个没用过. 安装 ...
- CSS简单布局总结
display block 块级元素,占据一行 none 隐藏 inline 允许同一行显示,但不再有宽和高 inline-block 允许在一行的块级元素,可 ...
- Web页中table导出到execl(带模板)
1.将excel另存为html,将其复制到aspx文件中 2.输出格式为excel InitData(); Response.Clear(); Response.Buffer = true; Resp ...
- having()方法设置查询条件,where()设置查询条件
having 和 where区别 ① 使用有先后顺序 ② where price>100 having price>100 ③ where 设置条件,字段必须是数据表中存在的 ...
- USACO 08-Nov( 最小生成树)
美国人出题拐弯抹角,倒是挺尊重动物的 10206301 2 52 3 52 4 123 4 172 5 153 5 64 5 12 Hint从牧场4起床, 然后按照 4, 5, 4, 2, 3, 2, ...
- SQL 变量
1.变量可以暂时储存数据 --定义变量: declare @xxx int --变量赋值: set @xxx=1 select @xxx=3 --变量的使用: print @xxx 2.--全 ...
- colormap
http://cn.mathworks.com/help/matlab/ref/colormap.html
- Windows 下字节转换
Windows 下字节转换 #include <string> #include <windows.h> class CharsConversion { public: sta ...
- jsp状态管理
http无状态协议 服务器记不住你 每次浏览器访问,服务器不会特点保存相应信息,故记不住你 jsp状态存储的两种机制 cookie 存储在客户端 用途: 1.简化登陆 2.追踪特定对象 3.保存用户常 ...
- 认识web前端
对于一个只是浅尝辄止c语言.学过汇编语言的我,思考了半年终于在这一天入了坑,学习web前端. web前端,看着这个名字好高大上,其实我目前的理解就是写页面,是各种图片动画文字在一个页面上呈现,再一点能 ...