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的更多相关文章

  1. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  2. Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  4. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. [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 ...

  6. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  7. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  8. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  9. 【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) ...

随机推荐

  1. 省常中模拟 day2

    第一题: 题目大意: 有mn颗糖,要装进k个盒子里,使得既可以平均分给n个人,也可以平均分给m个人. 求k的最小值. 解题过程: 1.先看一组小数据(13,21).那么根据贪心的原则很容易想到先拿13 ...

  2. 用maven搭建 testNG+PowerMock+Mockito测试框架

    单元测试是开发中必不可少的一部分,是产品代码的重要保证. Junit和testNG是当前最流行的测试框架,Junit是使用最广泛的测试框架,有兴趣的话自己baidu一下. testNG基于Junit和 ...

  3. jetty启动不能保存

    主要原因是jetty缓存的静态页面不能被修改.只需要在web.xml文件中配置如下: <servlet>    <!-- Override init parameter to avo ...

  4. find_in_set()

    $where[]="find_in_set('".$grades."',a.grades)";

  5. Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别

    初接触Hadoop技术的朋友肯定会对它体系下寄生的个个开源项目糊涂了,我敢保证Hive,Pig,HBase这些开源技术会把你搞的有些糊涂,不要紧糊涂的不止你一个,如某个菜鸟的帖子的疑问,when to ...

  6. Bootstrap框架基础

    特点:写非常少的代码 即可实现多终端的页面适配 ☑  简单灵活可用于架构流行的用户界面和交互接口的html.css.javascript工具集. ☑  基于html5.css3的bootstrap,具 ...

  7. [Python模式]策略模式

    策略模式 定义了算法族,分别封装起来,让它们之间可以互相替换.此模式让算法的变化独立于使用算法的客户. 作为动态语言,Python实现策略模式非常容易,只要所有算法提供相同的函数即可. import ...

  8. swift objective-及c语言 混编

    在xocde6出来我们大部分代码都是用objective-c写的(部分C/C++),现在出生来了一个新的语言叫swift,那么如何既能使用我们之前的代码,还可以使用新语言呢, 本文就此做一下说明. 关 ...

  9. C#—WebService

    一.qq是否在线 1.添加Web引用    qqOnlineWebService cn.com.webxml.www.qqOnlineWebService shelly1 = new NIIT1109 ...

  10. supervisor安装配置与使用

    supervisor:C/S架构的进程控制系统,可使用户在类UNIX系统中监控.管理进程.常用于管理与某个用户或项目相关的进程. 组成部分supervisord:服务守护进程supervisorctl ...