QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#874335#3445. Numbers On a Treefernandes_queilaWA 75ms44116kbJava171.9kb2025-01-28 01:53:132025-01-28 01:53:14

Judging History

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

  • [2025-01-28 01:53:14]
  • 评测
  • 测评结果:WA
  • 用时:75ms
  • 内存:44116kb
  • [2025-01-28 01:53:13]
  • 提交

answer

import java.util.Scanner;

public class BinarySearchTree {

    static class TreeNode {
        int value;
        TreeNode left, right;

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

    public static TreeNode buildTree(int height) {
        if (height < 0) {
            return null;
        }
        TreeNode root = new TreeNode(1);
        buildTreeRecursively(root, height, 1);
        return root;
    }

    private static void buildTreeRecursively(TreeNode node, int height, int currentValue) {
        if (height == 0) {
            return;
        }
        node.left = new TreeNode(currentValue * 2);
        node.right = new TreeNode(currentValue * 2 + 1);
        buildTreeRecursively(node.left, height - 1, currentValue * 2);
        buildTreeRecursively(node.right, height - 1, currentValue * 2 + 1);
    }

    public static int calculateLabel(TreeNode root, String path) {
        TreeNode currentNode = root;
        for (int i = 0; i < path.length(); i++) {
            char move = path.charAt(i);
            if (Character.toLowerCase(move) == 'l') {
                currentNode = currentNode.left;
            } else if (Character.toLowerCase(move) == 'r') {
                currentNode = currentNode.right;
            } else {
                throw new IllegalArgumentException("Invalid move: use 'L' or 'R'.");
            }
        }
        return currentNode.value;
    }

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

        String inputLine = scanner.nextLine().strip();
        String[] parts = inputLine.split(" ");

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

        TreeNode root = buildTree(height);
        int result = calculateLabel(root, path);
        System.out.println(result);

        scanner.close();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 75ms
memory: 44116kb

input:

3 LR

output:

5

result:

wrong answer 1st lines differ - expected: '11', found: '5'