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?


/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
void swap(int& a,int&b)
{
int tmp=a;
a=b;
b=tmp;
}
TreeNode* findlarge(TreeNode* root,int val)
{
if(root==NULL) return NULL;
TreeNode* result=NULL;
if(root->val>val)
{
val=root->val;
result=root;
}
TreeNode* p1=findlarge(root->left,val);
TreeNode* p2=findlarge(root->right,val);
if(p1!=NULL) result=p1;
if(p2!=NULL)
{
if(result==NULL) result=p2;
else
if(result->val<p2->val) result=p2;
}
return result;
}
TreeNode* findsmall(TreeNode* root,int val)
{
if(root==NULL) return NULL;
TreeNode* result=NULL;
if(root->val<val)
{
val=root->val;
result=root;
}
TreeNode* p1=findsmall(root->left,val);
TreeNode* p2=findsmall(root->right,val);
if(p1!=NULL) result=p1;
if(p2!=NULL)
{
if(result==NULL) result=p2;
else
if(result->val>p2->val) result=p2;
}
return result;
}
public:
void recoverTree(TreeNode *root)
{
if(root==NULL) return;
TreeNode* left=findlarge(root->left,root->val);
TreeNode* right=findsmall(root->right,root->val);
if(left!=NULL && right!=NULL)
{
swap(left->val,right->val);
return;
}
if(right!=NULL)
{
swap(root->val,right->val);
return;
}
if(left!=NULL)
{
swap(root->val,left->val);
return;
}
recoverTree(root->left);
recoverTree(root->right);
}
};

Recover Binary Search Tree,布布扣,bubuko.com

时间: 2024-12-14 01:41:39

Recover Binary Search Tree的相关文章

LeetCode: Recover Binary Search Tree

LeetCode: 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 s

【leetcode】Recover Binary Search Tree

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? 中序

LeetCode详细分析 :: Recover Binary Search Tree [Tree]

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? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

39. Recover Binary Search Tree &amp;&amp; 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 prett

leetcode 之 Recover Binary Search Tree

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? c

LeetCode: Recover Binary Search Tree [099]

[题目] 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? confused what "{1,#,2,3

099 Recover Binary Search Tree

099 Recover Binary Search Tree 没有按照要求用 constant space.... 用valid BST 找出两个not in order 的nodes class Solution: def recoverTree(self, root): [fN, sN] = self.valid(root) fN.val, sN.val = sN.val, fN.val def valid(self, root): pre, fN, sN = TreeNode(-10000

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

Recover Binary Search Tree leetcode java

题目: 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? 题解: 解决方法是利用中序遍历找顺序不对的两个点