QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873917#3445. Numbers On a Treefernandes_queilaCompile Error//Python31.5kb2025-01-27 09:01:002025-01-27 09:01:01

Judging History

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

  • [2025-01-27 09:01:01]
  • 评测
  • [2025-01-27 09:01:00]
  • 提交

answer

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine().trim();
        String[] parts = input.split(" ");

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

        BalancedTree balancedTree = new BalancedTree(height);
        int result = balancedTree.findNodeLabel(height, path);
        System.out.println(result);
        scanner.close();
    }
}

class BalancedTree {
    private TreeNode root;

    public BalancedTree(int height) {
        root = constructTree(height);
    }

    private TreeNode constructTree(int height) {
        int totalNodes = (1 << (height + 1)) - 1;
        return buildTree(1, totalNodes);
    }

    private TreeNode buildTree(int start, int end) {
        if (start > end) return null;
        int mid = (start + end) / 2;
        TreeNode node = new TreeNode(mid);
        node.left = buildTree(start, mid - 1);
        node.right = buildTree(mid + 1, end);
        return node;
    }


    public int findNodeLabel(int height, String path) {
        int cur = 1;
        for (char c : path.toCharArray()) {
            cur = cur * 2 + (c == 'R' ? 1 : 0);
        }
        int totalNodes = (1 << (height + 1)) - 1;
        return totalNodes - cur + 1;
    }
}

class TreeNode {
    int value;
    TreeNode left, right;

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

Details

  File "answer.code", line 3
    public class Main {
           ^^^^^
SyntaxError: invalid syntax