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. XML学习摘要

    XML元素可以在开始标签中包含属性. 属性(Attribute)提供关于元素的额外信息,属性必须加引号. 属性值必须被引号包围,不过单引号和双引号均可,若属性值本身包含双引号,那么有必要使用单引号包围 ...

  2. jquery 动态生成html后click事件不触发原因

    转自:http://www.iam3y.com/html/560.html 最近在做一个项目的时候,遇到动态加载微博内容,然后点击“展开评论”后获取该微博的所有评论.这里使用了动态加载的<spa ...

  3. sprite图在移动端的使用

    做移动端页面时,设计稿中的切片图片往往是实际的2倍,此处记录图片正常显示大小的技巧. 当图片是单张的话,可以对容器设计实际大小,然后设置background-image,为了让图片缩小一倍,可以设置b ...

  4. spirng线程池的配置与使用

    1.在xml中配置线程池 <!-- 配置线程池 --> <bean id="taskExecutor" class="org.springframewo ...

  5. candence 知识积累1

    Allegro 总结: 1.防焊层(Solder Mask):又称绿油层,PCB非布线层,用于制成丝网印板,将不需要焊接的地方涂上防焊剂.在防焊层上预留的焊盘大小要比实际的焊盘大一些,其差值一般为10 ...

  6. 简单通过java的socket&serversocket以及多线程技术实现多客户端的数据的传输,并将数据写入hbase中

    业务需求说明,由于公司数据中心处于刚开始部署的阶段,这需要涉及其它部分将数据全部汇总到数据中心,这实现的方式是同上传json文件,通过采用socket&serversocket实现传输. 其中 ...

  7. 帐户当前被锁定,所以用户 sa 登录失败。系统管理员无法将该帐户解锁 解决方法

    ALTER LOGIN sa ENABLE ; GO ALTER LOGIN sa WITH PASSWORD = 'password' unlock, check_policy = off, che ...

  8. C# 获取MAC地址

    /********************************************************************** * C# 获取MAC地址 * 说明: * 在C#中获取本 ...

  9. img标签中alt和title属性的正确使用

    在的img标签有两个属性分别为alt和title,对于很多初学者而言对这两个属性的正确使用都还抱有迷惑,当然这其中一部分原因也是ie浏览器所导致的.正确的使用这两个属性除了可以提高图片的搜索能力外,在 ...

  10. Oracle学习系列5

    Oracle学习系列5 ************************************************************************************ ,掌握 ...