LeetCode99 Recover Binary Search Tree

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

Recover the tree without changing its structure. (Hard)

Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

分析:

BST的中序遍历应该是递增的,我们考虑这样一个中序遍历序列1,2,6,4,5,3,7,其中3,6是交换了位置的。

所以我们就按照中序遍历的顺序遍历树,记录cur, prev, beforePrev三个变量;

第一次出现before < prev > cur的prev即为要交换的第一个元素,最后一个满足beforePrev > prev < cur的即为要交换的另一个元素。

然后再遍历一遍把这两个节点找出来,交换其value值即可。

注意:比如1,0这种样例,当最后一个节点是被交换的元素的时候,无法用上述判断,但如果其满足prev > cur说明cur即为要交换的元素。

代码:

 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     int beforePrev = -0x7FFFFFFF, prev = -0x7FFFFFFF, cur = -0x7FFFFFFF;
12     int s1 = -0x7FFFFFFF, s2 = -0x7FFFFFFF;
13     TreeNode* t1;
14     TreeNode* t2;
15 private:
16     void helper(TreeNode* root) {
17         if (root == nullptr) {
18             return;
19         }
20         helper(root -> left);
21         beforePrev = prev;
22         prev = cur;
23         cur = root -> val;
24         if (beforePrev < prev && prev > cur && s1 == -0x7FFFFFFF) {
25             s1 = prev;
26         }
27         if (beforePrev > prev && prev < cur ) {
28             s2 = prev;
29         }
30
31         helper(root -> right);
32     }
33
34     void dfs(TreeNode* root) {
35         if (root == nullptr) {
36             return;
37         }
38         dfs(root -> left);
39         if (root -> val == s1) {
40             t1 = root;
41         }
42         if (root -> val == s2) {
43             t2 = root;
44         }
45         dfs(root -> right);
46     }
47 public:
48     void recoverTree(TreeNode* root) {
49         helper(root);
50         if (cur < prev) {
51             s2 = cur;
52         }
53         dfs(root);
54         swap(t1 -> val, t2 -> val);
55         return;
56     }
57 };
时间: 2024-10-22 01:43:01

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

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 t

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树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两