-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecover_binary_search_tree.cpp
More file actions
113 lines (101 loc) · 2.92 KB
/
recover_binary_search_tree.cpp
File metadata and controls
113 lines (101 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* =====================================================================================
*
* Filename: recover_binary_search_tree.cpp
*
* Description: 99. Recover Binary Search Tree.
* https://leetcode.com/problems/recover-binary-search-tree/
*
* Version: 1.0
* Created: 02/25/23 12:13:43
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <stack>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
static TreeNode* convert(const std::vector<int*>& nums, const int i = 0) {
if (i >= nums.size() || nums[i] == nullptr) {
return nullptr;
}
TreeNode* root = new TreeNode(*nums[i]);
root->left = convert(nums, 2 * i + 1);
root->right = convert(nums, 2 * i + 2);
return root;
}
static std::vector<int> visitInorder(TreeNode* root) {
std::vector<int> nums;
std::stack<TreeNode*> nodes;
while (root != nullptr || !nodes.empty()) {
while (root != nullptr) {
nodes.push(root);
root = root->left;
}
root = nodes.top();
nums.push_back(root->val);
nodes.pop();
root = root->right;
}
return nums;
}
};
// In-order traversal by depth first search
class Solution {
public:
void recoverTree(TreeNode* root) {
dfsTraversal(root);
std::swap(first->val, second->val);
}
private:
void dfsTraversal(TreeNode* root) {
if (root == nullptr) {
return;
}
dfsTraversal(root->left);
if (first == nullptr && prev != nullptr && prev->val > root->val) {
first = prev;
}
if (first != nullptr && prev->val > root->val) {
second = root;
}
prev = root;
dfsTraversal(root->right);
}
private:
TreeNode* prev = nullptr;
TreeNode* first = nullptr;
TreeNode* second = nullptr;
};
TEST(Solution, recoverTree) {
#define _N(x) new int(x)
std::vector<std::pair<TreeNode*, std::vector<int>>> cases = {
std::make_pair(TreeNode::convert(std::vector<int*>{_N(1), _N(3), nullptr,
nullptr, _N(2)}),
std::vector<int>{1, 2, 3}),
std::make_pair(TreeNode::convert(std::vector<int*>{
_N(3), _N(1), _N(4), nullptr, nullptr, _N(2)}),
std::vector<int>{1, 2, 3, 4}),
};
for (auto& c : cases) {
Solution().recoverTree(c.first);
EXPECT_THAT(TreeNode::visitInorder(c.first),
testing::ElementsAreArray(c.second));
}
#undef _N
}