2022.09.01 Bilibili Java开发笔试
好像后端有几套试卷,我和我同学做的都不一样
我的是 “JAVA开发工程师-人事产研组【2023届】”
10 分 单选 * 5
20 分 多选 * 5
70 分 编程 * 3
编程题一个链表,两个树
鼠鼠的第三题实在没啥思路,水分提前跑路了。
public int maxValue(TreeNode root) {
// 记录每一层能添加的最大值
int[] change = new int[500001];
Deque<TreeNode> deque = new ArrayDeque<>();
// 记录每一层的和
List<Integer> sum = new ArrayList<>();
deque.addLast(root);
// 深度
int depth = 0;
while (!deque.isEmpty()) {
int size = deque.size();
int curSum = 0, max = 0, min = 0;
while (size-- > 0) {
TreeNode cur = deque.pollFirst();
curSum += cur.val;
// 判断该节点的子节点交换上来,该层能不能变得更大
int left = 0, right = 0;
if (cur.left != null) {
left = cur.left.val - cur.val;
deque.addLast(cur.left);
}
if (cur.right != null) {
right = cur.right.val - cur.val;
deque.addLast(cur.right);
}
max = Math.max(max, Math.max(left, right));
change[depth] = Math.max(change[depth], max);
// 判断该节点换到下一层,下一层能不能变得更大
left = -left;
right = -right;
if (cur.left != null) {
change[depth + 1] = Math.max(change[depth + 1], Math.max(left, right));
}
if (cur.right != null) {
change[depth + 1] = Math.max(change[depth + 1], Math.max(left, right));
}
}
sum.add(curSum);
depth++;
}
int max = Integer.MIN_VALUE;
// 将每层之和,以及能添加的值 取最大值
for (int i = 0; i < sum.size(); i++) {
max = Math.max(sum.get(i) + Math.max(0, change[i]), max);
}
return max;
}