LeetCode – Refresh – Recover Binary Search Tree

本文介绍两种不同的方法来恢复被错误修改节点值的二叉搜索树,使其重新符合二叉搜索树的性质。第一种方法使用两个向量分别存储遍历过程中遇到的节点和值,并对值进行排序后重新赋值给节点。第二种方法则采用O(1)的空间复杂度,利用中序遍历的过程中检查当前节点值是否大于前一个节点值,从而找出并交换错误的节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Use two vectors to store the nodes and values. Sort the values.

 1 /**
 2  * Definition for binary tree
 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 getTree(vector<int> &result, vector<TreeNode *> &nodes, TreeNode *root) {
13         if (!root) return;
14         getTree(result, nodes, root->left);
15         result.push_back(root->val);
16         nodes.push_back(root);
17         getTree(result, nodes, root->right);
18     }
19     void recoverTree(TreeNode *root) {
20         vector<int> nums;
21         vector<TreeNode *> nodes;
22         getTree(nums, nodes, root);
23         sort(nums.begin(), nums.end());
24         for (int i = 0; i < nums.size(); i++) {
25             nodes[i]->val = nums[i];
26         }
27     }
28 };

 

2. O(1) Memory

Reference my previous Inorder Traversal

Just add a check whether the previous node is larger than current node.

 1 /**
 2  * Definition for binary tree
 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         if (!root) return;
14         TreeNode *n1, *n2, *parent, *prev;
15         bool found = false;
16         while (root) {
17             if (!root->left) {
18                 if (parent && parent->val > root->val) {
19                     if (!found) {
20                         n1 = parent;
21                         found = true;
22                     }
23                     n2 = root;
24                 }
25                 parent = root;
26                 root = root->right;
27             } else {
28                 prev = root->left;
29                 while (prev->right && prev->right != root) prev = prev->right;
30                 if (!prev->right) {
31                     prev->right = root;
32                     root = root->left;
33                 } else {
34                     if (parent && parent->val > root->val) {
35                         if (!found) {
36                             n1 = parent;
37                             found = true;
38                         }
39                         n2 = root;
40                     }
41                     parent = root;
42                     prev->right = NULL;
43                     root = root->right;
44                 }
45             }
46         }
47         if (n1 && n2) {
48             int t = n1->val;
49             n1->val = n2->val;
50             n2->val = t;
51         }
52     }
53 };

 

转载于:https://www.cnblogs.com/shuashuashua/p/4357435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值