QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#873307 | #3445. Numbers On a Tree | fernandes_queila | TL | 81ms | 47860kb | Java21 | 2.1kb | 2025-01-26 11:42:28 | 2025-01-26 11:42:33 |
Judging History
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 - 1, "");
}
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 rootLabel = (1 << (height + 1)) - 1;
int label = rootLabel;
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);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 72ms
memory: 46404kb
input:
3 LR
output:
11
result:
ok single line: '11'
Test #2:
score: 0
Accepted
time: 71ms
memory: 46544kb
input:
3 RRL
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 73ms
memory: 46628kb
input:
2
output:
7
result:
ok single line: '7'
Test #4:
score: 0
Accepted
time: 81ms
memory: 47860kb
input:
3 LRL
output:
6
result:
ok single line: '6'
Test #5:
score: -100
Time Limit Exceeded
input:
26 RRLLLLRRRL