第三题为什么啊,感觉题目理解上有偏差,还有楼主你的visited数组是多余的... 我贴个代码,帮忙看下 import java.util.Scanner; public class Main090903 { private static long max = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[][] tree = new long[n + 1][3]; for (int i = 1; i <= n; i++) { in.nextInt(); // id tree[i][0] = in.nextLong(); // value tree[i][1] = in.nextInt(); // left node id tree[i][2] = in.nextInt(); // right node id } dfs(tree, 1, 0); System.out.println(max); } public static void dfs (long[][] tree, int currNode, long sum) { long newSum = sum ^ tree[currNode][0]; if (newSum > max) max = newSum; if (tree[currNode][1] != -1) { dfs(tree, (int)tree[currNode][1], newSum); dfs(tree, (int)tree[currNode][1], 0); } if (tree[currNode][2] != -1) { dfs(tree, (int)tree[currNode][2], newSum); dfs(tree, (int)tree[currNode][2], 0); } } }
点赞 5

相关推荐

牛客网
牛客企业服务