QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#873197#3445. Numbers On a Treefernandes_queilaWA 0ms1664kbC991.9kb2025-01-26 10:22:402025-01-26 10:22:41

Judging History

你现在查看的是最新测评结果

  • [2025-01-26 10:22:41]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:1664kb
  • [2025-01-26 10:22:40]
  • 提交

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'