LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

首先是O(N)空间的方法,用递归:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     void recoverTree(TreeNode* root) {
13         vt.clear();
14         vi.clear();
15         if(!root) return;
16         tranverse(root);
17         sort(vi.begin(), vi.end());
18         for_each(vt.begin(), vt.end(), [this](TreeNode * t){static int i = 0; t->val = vi[i++];});
19     }
20
21     void tranverse(TreeNode * root)
22     {
23         if(!root) return;
24         tranverse(root->left);
25         vt.push_back(root);
26         vi.push_back(root->val);
27         tranverse(root->right);
28     }
29 private:
30     vector<TreeNode *> vt;
31     vector<int> vi;
32 };

下面这个方法是看别人实现的,想法很好,维护两个指针,一个指向前面一个违反条件的,一个指向后面一个,q不断的进行更新,代码如下:

 1 class Solution {
 2 public:
 3     void recoverTree(TreeNode* root)
 4     {
 5         p = q = prev = NULL;
 6         if(!root) return;
 7         tranverse(root);
 8         swap(p->val, q->val);
 9     }
10
11     void tranverse(TreeNode * root)
12     {
13         if(!root) return ;
14         tranverse(root->left);
15         if(prev && (prev->val > root->val)){
16             if(!p) p = prev;//这里应该注意
17             q = root;//注意为什么q取的是root
18         }
19         prev = root;
20         tranverse(root->right);
21     }
22 private:
23     TreeNode * p, * q;
24     TreeNode * prev;
25 };
时间: 2024-10-04 00:40:01

LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)的相关文章

[LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

[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 solution? confused what "{1,#,2,3}"

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up:Could you do it using only constant space complexity? 给一个数组,验证是否为一个二叉搜索树的

[CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原理,请参见我之前的博客Validate Binary Search Tree 验证二叉搜索树.

LeetCode OJ - 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? 解题思路: 中序遍历BST,在遍历过程中记录出现错

[LeetCode] 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

[LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

[leetcode]108. Convert Sorted Array to Binary Search Tree构建二叉搜索树

构建二叉搜索树 /* 利用二叉搜索树的特点:根节点是中间的数 每次找到中间数,左右子树递归子数组 */ public TreeNode sortedArrayToBST(int[] nums) { return builder(nums,0,nums.length-1); } public TreeNode builder(int[] nums,int left,int right) { if (left>right) return null; int mid = (left+right)/2;

669. Trim a Binary Search Tree 修剪二叉搜索树

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R](R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary

669. Trim a Binary Search Tree修剪二叉搜索树

[抄题]: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed