QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#873197 | #3445. Numbers On a Tree | fernandes_queila | WA | 0ms | 1664kb | C99 | 1.9kb | 2025-01-26 10:22:40 | 2025-01-26 10:22:41 |
Judging History
answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Estrutura do n\xf3 da \xe1rvore
typedef struct Node {
int label;
struct Node* left;
struct Node* right;
} Node;
// Fun\xe7\xe3o para criar um novo n\xf3
Node* create_node(int label) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->label = label;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
// Fun\xe7\xe3o para inserir n\xf3s na \xe1rvore (BST)
Node* insert_node(Node* root, int label) {
if (root == NULL) {
return create_node(label);
}
if (label < root->label) {
root->left = insert_node(root->left, label);
} else if (label > root->label) {
root->right = insert_node(root->right, label);
}
return root;
}
// Fun\xe7\xe3o para construir a \xe1rvore com base na altura
Node* build_tree(int height) {
int num_nodes = (1 << (height + 1)) - 1; // N\xfamero total de n\xf3s
Node* root = NULL;
for (int i = 1; i <= num_nodes; i++) {
root = insert_node(root, i);
}
return root;
}
// Fun\xe7\xe3o para procurar o n\xf3 de acordo com o caminho
int search_node(Node* root, const char* road) {
Node* current = root;
int len = strlen(road);
for (int i = 0; i < len; i++) {
if (road[i] == 'L' || road[i] == 'l') {
if (current->left != NULL) {
current = current->left;
} else {
return -1; // Caminho inv\xe1lido
}
} else if (road[i] == 'R' || road[i] == 'r') {
if (current->right != NULL) {
current = current->right;
} else {
return -1; // Caminho inv\xe1lido
}
}
}
return current->label;
}
int main() {
char line[100];
fgets(line, sizeof(line), stdin);
int height;
char road[100] = "";
sscanf(line, "%d %s", &height, road);
Node* root = build_tree(height);
int result = search_node(root, road);
printf("%d\n", result);
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 1664kb
input:
3 LR
output:
-1
result:
wrong answer 1st lines differ - expected: '11', found: '-1'