QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#874335 | #3445. Numbers On a Tree | fernandes_queila | WA | 75ms | 44116kb | Java17 | 1.9kb | 2025-01-28 01:53:13 | 2025-01-28 01:53:14 |
Judging History
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();
}
}
詳細信息
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'