QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873273#3445. Numbers On a Treefernandes_queilaTL 80ms47948kbJava212.0kb2025-01-26 11:17:572025-01-26 11:17:57

Judging History

This is the latest submission verdict.

  • [2025-01-26 11:17:57]
  • Judged
  • Verdict: TL
  • Time: 80ms
  • Memory: 47948kb
  • [2025-01-26 11:17:57]
  • Submitted

answer

import java.util.Scanner;

public class BinaryTree {
    static class TreeNode {
        int label;
        TreeNode left;
        TreeNode right;

        TreeNode(int label) {
            this.label = label;
            left = right = null;
        }
    }

    TreeNode root;

    void buildTree(int height) {
        int maxNode = (1 << (height + 1)) - 1;
        root = new TreeNode(maxNode);
        buildTreeRec(root, height, "");
    }


    void buildTreeRec(TreeNode node, int height, String path) {
        if (height == 0) {
            return;
        }

        int local = 0;
        for (char move : path.toCharArray()) {
            if (move == 'L' || move == 'l') {
                local = 2 * local + 1;
            } else if (move == 'R' || move == 'r') {
                local = 2 * local + 2;
            }
        }

        int leftLabel = node.label - (local * 2 + 1);
        int rightLabel = node.label - (local * 2 + 2);

        node.left = new TreeNode(leftLabel);
        node.right = new TreeNode(rightLabel);

        buildTreeRec(node.left, height - 1, path + "L");
        buildTreeRec(node.right, height - 1, path + "R");
    }

    int searchNode(int height, String path) {
        int root = (1 << (height + 1)) - 1;
        int label = root;
        int local = 0;

        for (char move : path.toCharArray()) {
            if (move == 'L' || move == 'l') {
                local = 2 * local + 1;
            } else if (move == 'R' || move == 'r') {
                local = 2 * local + 2;
            }
        }

        label -= local;
        return label;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        scanner.close();

        String[] parts = input.split(" ");
        int height = Integer.parseInt(parts[0]);
        String path = parts.length > 1 ? parts[1] : "";

        BinaryTree tree = new BinaryTree();
        tree.buildTree(height);
        int label = tree.searchNode(height, path);

        System.out.println(label);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 80ms
memory: 47948kb

input:

3 LR

output:

11

result:

ok single line: '11'

Test #2:

score: 0
Accepted
time: 75ms
memory: 46780kb

input:

3 RRL

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 69ms
memory: 46624kb

input:

2

output:

7

result:

ok single line: '7'

Test #4:

score: 0
Accepted
time: 74ms
memory: 46764kb

input:

3 LRL

output:

6

result:

ok single line: '6'

Test #5:

score: -100
Time Limit Exceeded

input:

26 RRLLLLRRRL

output:


result: