首页 > 代码库 > 39. Recover Binary Search Tree && Validate Binary Search Tree
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。