-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1496.判断路径是否相交.c
More file actions
121 lines (86 loc) · 2.19 KB
/
1496.判断路径是否相交.c
File metadata and controls
121 lines (86 loc) · 2.19 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
114
115
116
117
118
119
120
/*
* @lc app=leetcode.cn id=1496 lang=c
*
* [1496] 判断路径是否相交
*/
// @lc code=start
struct TREE_NODE {
int node_value;
struct TREE_NODE *left_tree;
struct TREE_NODE *right_tree;
} tree_node;
bool treeInsert(struct TREE_NODE *root, int node_value)
{
struct TREE_NODE *node = malloc(sizeof(struct TREE_NODE));
node->node_value = node_value;
node->left_tree = NULL;
node->right_tree = NULL;
while (root->node_value != node->node_value) {
if (root->node_value > node->node_value) {
if (root->left_tree != NULL) {
root = root->left_tree;
}
else {
root->left_tree = node;
return false;
}
}
else if (root->node_value < node->node_value) {
if (root->right_tree != NULL) {
root = root->right_tree;
}
else {
root->right_tree = node;
return false;
}
}
}
return true;
}
void treeErase(struct TREE_NODE *root)
{
return ;
}
struct TREE_NODE *treeInit(int node_value)
{
struct TREE_NODE *root = malloc(sizeof(struct TREE_NODE));
root->left_tree = NULL;
root->right_tree = NULL;
root->node_value = node_value;
return root;
}
bool isPathCrossing(char * path){
struct TREE_NODE *searchTreeRoot = treeInit(0);
int site = 0;
for (char *pathPointer = path; pathPointer < (path + strlen(path)); pathPointer++) {
switch (*pathPointer) {
case 'N': {
site += 1;
break;
}
case 'E': {
site += 10001;
break;
}
case 'S': {
site -= 1;
break;
}
case 'W': {
site -= 10001;
break;
}
default : {
// 异常
break;
}
}
if (treeInsert(searchTreeRoot, site)) {
return true;
}
}
// 清空二叉搜索树
treeErase(searchTreeRoot);
return false;
}
// @lc code=end