华为OD机试真题 - 最富裕的小家庭
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = Integer.parseInt(in.nextLine());
String str = in.nextLine();
int[] assets = Arrays.stream(str.split(" ")).mapToInt(Integer::parseInt).toArray();
int[][] map = new int[num - 1][2];
for (int i = 0; i < num - 1; i++) {
map[i] = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
Node head = buildTree(map, assets);
// printTree(head);
System.out.println(getMaxFamlie(head));
}
public static int getMaxFamlie(Node head) {
if (head == null)
return 0;
int max = 0;
max += head.value;
if (head.left != null)
max += head.left.value;
if (head.right != null)
max += head.right.value;
max = Math.max(getMaxFamlie(head.left), max);
max = Math.max(getMaxFamlie(head.right), max);
return max;
}
public static Node buildTree(int[][] map, int[] assets) {
int count = 0;
Node[] nodes = new Node[assets.length];
for (int i = 0; i < assets.length; i++) {
nodes[i] = new Node(assets[i]);
}
for (int i = 0; i < map.length; i++) {
if (nodes[map[i][0] - 1].left == null)
nodes[map[i][0] - 1].left = nodes[map[i][1] - 1];
else nodes[map[i][0] - 1].right = nodes[map[i][1] - 1];
}
return nodes[0];
}
public static void printTree(Node head) {
if (head == null)
return;
System.out.println(head.value);
printTree(head.left);
printTree(head.right);
}
static class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
}
Scanner in = new Scanner(System.in);
int num = Integer.parseInt(in.nextLine());
String str = in.nextLine();
int[] assets = Arrays.stream(str.split(" ")).mapToInt(Integer::parseInt).toArray();
int[][] map = new int[num - 1][2];
for (int i = 0; i < num - 1; i++) {
map[i] = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
Node head = buildTree(map, assets);
// printTree(head);
System.out.println(getMaxFamlie(head));
}
public static int getMaxFamlie(Node head) {
if (head == null)
return 0;
int max = 0;
max += head.value;
if (head.left != null)
max += head.left.value;
if (head.right != null)
max += head.right.value;
max = Math.max(getMaxFamlie(head.left), max);
max = Math.max(getMaxFamlie(head.right), max);
return max;
}
public static Node buildTree(int[][] map, int[] assets) {
int count = 0;
Node[] nodes = new Node[assets.length];
for (int i = 0; i < assets.length; i++) {
nodes[i] = new Node(assets[i]);
}
for (int i = 0; i < map.length; i++) {
if (nodes[map[i][0] - 1].left == null)
nodes[map[i][0] - 1].left = nodes[map[i][1] - 1];
else nodes[map[i][0] - 1].right = nodes[map[i][1] - 1];
}
return nodes[0];
}
public static void printTree(Node head) {
if (head == null)
return;
System.out.println(head.value);
printTree(head.left);
printTree(head.right);
}
static class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
}
全部评论
相关推荐
01-23 15:35
University of Edinburgh 嵌入式软件工程师 不知道怎么取名字_:嵌入式其实不是很好干的,要学的东西比较多的,你这个c stm32都是比较基础的了
点赞 评论 收藏
分享
2025-12-22 16:53
大连理工大学 产品总监
王海:不算mentor但也带过几个实习生,直接观感就是你可以摸鱼可以想早下班,分给你的工作好好完成就行 点赞 评论 收藏
分享