-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0230_kthSmallest.cc
91 lines (85 loc) · 2.27 KB
/
Problem_0230_kthSmallest.cc
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
#include <iostream>
#include <vector>
#include "UnitTest.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) {}
};
class Solution
{
public:
// Morris遍历
// 整个遍历的过程跟中序遍历顺序一致
int kthSmallest(TreeNode *root, int k)
{
if (root == nullptr)
{
return -1;
}
TreeNode *cur = root;
TreeNode *mostRight = nullptr;
int index = 1;
while (cur != nullptr)
{
// 先从遍历cur的左孩子
mostRight = cur->left;
if (mostRight != nullptr)
{
while (mostRight->right != nullptr && mostRight->right != cur)
{
// 找到cur的左孩子的最右节点
mostRight = mostRight->right;
}
if (mostRight->right == nullptr)
{
// 第一次遍历到,就把这个节点的右孩子指向cur
// 这个思维符合中序遍历,mostRight是cur前面的一个节点
mostRight->right = cur;
// 一直往左,先把每个cur节点的mostRight指向cur
cur = cur->left;
continue;
}
else
{
// 第二次回溯遍历到,恢复这个节点的指针
mostRight->right = nullptr;
}
}
// 到这里左孩子已经遍历完毕
if (index++ == k)
{
return cur->val;
}
// 开始遍历cur的右孩子
cur = cur->right;
}
return -1;
}
};
void testKthSmallest()
{
Solution s;
TreeNode *t4 = new TreeNode(2, nullptr, nullptr);
TreeNode *t3 = new TreeNode(4, nullptr, nullptr);
TreeNode *t2 = new TreeNode(1, nullptr, t4);
TreeNode *t1 = new TreeNode(3, t2, t3);
TreeNode *r6 = new TreeNode(1, nullptr, nullptr);
TreeNode *r5 = new TreeNode(4, nullptr, nullptr);
TreeNode *r4 = new TreeNode(2, r6, nullptr);
TreeNode *r3 = new TreeNode(6, nullptr, nullptr);
TreeNode *r2 = new TreeNode(3, r4, r5);
TreeNode *r1 = new TreeNode(5, r2, r3);
EXPECT_EQ_INT(1, s.kthSmallest(t1, 1));
EXPECT_EQ_INT(3, s.kthSmallest(r1, 3));
EXPECT_SUMMARY;
}
int main()
{
testKthSmallest();
return 0;
}