50068. Tree Traversal

難度:4.4/5

注意紀錄的東西是從root到你的current就好,不用把每一步都記下來。

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
#include <stdio.h>
#include "tree.h"

void print_node(Node* n){
printf("%d\n", n->label);
}

void traversal(Node *root, int N, int command[]){
Node* history[1024];
int idx = 0;
history[idx] = root;
for(int i = 0; i < N; i++){
int t = command[i];
if(t == 0){
print_node(history[idx]);
break;
}
else if(t == 1){
print_node(history[idx]);
}
else if(t == 2){
if(idx == 0){
print_node(history[idx]);
break;
}
else{
idx -= 1;
}
}
else if(t == 3){
if(history[idx]->left == NULL){
print_node(history[idx]);
break;
}
else{
history[idx + 1] = history[idx]->left;
idx += 1;
}
}
else if(t == 4){
if(history[idx]->right == NULL){
print_node(history[idx]);
break;
}
else{
history[idx + 1] = history[idx]->right;
idx += 1;
}

}
else if(t == 5){
if(idx == 0){
print_node(history[idx]);
break;
}
else if(history[idx - 1]->left == NULL || history[idx - 1]->right == NULL){
print_node(history[idx]);
break;
}
else{
if(history[idx] == history[idx - 1]->left){
history[idx] = history[idx - 1]->right;
}
else{
history[idx] = history[idx - 1]->left;
}
}
}
}
}


50068. Tree Traversal
https://aaronlin1229.github.io/judgegirl_50068/
Author
Akizumi
Posted on
July 17, 2023
Licensed under