不知道为什么只通过了第二组数据,把另一个Java AC的代码也贴出来比较一下吧。
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:1238
解决:273
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
每个测试案例包括n+1行:
第一行为2个整数n,k(1<=n<=10000),n表示结点的个数,k表示要求的路径和,结点编号从1到n。
接下来有n行。这n行中每行为3个整数vi,leftnode,rightnode,vi表示第i个结点的值,leftnode表示第i个结点的左孩子结点编号,rightnode表示第i个结点的右孩子结点编号,若无结点值为-1。编号为1的结点为根结点。
对应每个测试案例,先输出“result:”占一行,接下来按字典顺序输出满足条件的所有路径,这些路径由结点编号组成,输出格式参照输出样例。
5 2210 2 35 4 512 -1 -14 -1 -17 -1 -11 51 -1 -1
result:A path is found: 1 2 5A path is found: 1 3result:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class S25 { public static void main(String[] args) throws FileNotFoundException { BufferedInputStream in = new BufferedInputStream(new FileInputStream("in.in")); System.setIn(in); Scanner cin = new Scanner(System.in); while (cin.hasNextInt()) { int n = cin.nextInt(); int k = cin.nextInt(); TreeNode[] tree = new TreeNode[n+1]; for(int i=0; i<n; i++){ if(tree[i] == null){ tree[i] = new TreeNode(cin.nextInt(), i+1); } int leftIdx = cin.nextInt(); int rightIdx = cin.nextInt(); if(leftIdx > rightIdx){ int tmp = leftIdx; leftIdx = rightIdx; rightIdx = tmp; } tree[i].left = new TreeNode(0, leftIdx); tree[i].right = new TreeNode(0, rightIdx); } for(int i=0; i<n; i++){ if(tree[i].left.idx != -1){ tree[i].left = tree[tree[i].left.idx-1]; }else{ tree[i].left = null; } if(tree[i].right.idx != -1){ tree[i].right = tree[tree[i].right.idx-1]; }else{ tree[i].right = null; } } // preorder(tree[0]); ArrayList<ArrayList<Integer>> ret = printPath(tree[0], k); System.out.println("result:"); for (ArrayList<Integer> al : ret) { System.out.print("A path is found:"); for (Integer val : al) { System.out.print(" " + val); } System.out.println(); } } } private static ArrayList<ArrayList<Integer>> printPath(TreeNode root, int k) { ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> al = new ArrayList<Integer>(); rec(root, k, al, ret); return ret; } private static void rec(TreeNode root, int k, ArrayList<Integer> al, ArrayList<ArrayList<Integer>> ret){ if(root == null){ return; } if(root.val == k){ al.add(root.idx); ret.add(new ArrayList<Integer>(al)); al.remove(al.size()-1); return; } al.add(root.idx); rec(root.left, k-root.val, al, ret); rec(root.right, k-root.val, al, ret); al.remove(al.size()-1); } private static void preorder(TreeNode root){ if(root != null){ System.out.print(root.val + " "); preorder(root.left); preorder(root.right); } } public static class TreeNode{ int val; int idx; TreeNode left, right; public TreeNode(int val_, int idx_){ val = val_; idx = idx_; } } }
AC:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Stack; public class Main { /* * 1368 */ private static BinaSortTree[] biTrees; private static int valueArr[]; private static int k ; public static void main(String[] args) throws Exception { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); while (st.nextToken() != StreamTokenizer.TT_EOF) { int n = (int) st.nval; st.nextToken(); k = (int) st.nval; biTrees = new BinaSortTree[n+1]; valueArr = new int[n+1]; for (int i = 1; i < n+1; i++) { st.nextToken(); valueArr[i] = (int)st.nval; st.nextToken(); int left = (int)st.nval; st.nextToken(); int right = (int)st.nval; BinaSortTree leftTree = null; BinaSortTree rightTree = null; if (left != -1 && right != -1) { int temp = left; left = Math.min(left, right); right = Math.max(temp, right); } if (left != -1) { leftTree = new BinaSortTree(left); } if (right != -1) { rightTree = new BinaSortTree(right); } biTrees[i] = new BinaSortTree(leftTree, rightTree ,i); } createTree(biTrees[1]); int sum = 0; Stack<Integer> stack = new Stack<Integer>(); System.out.println("result:"); printPath(biTrees[1] , sum, stack ); } } private static void createTree(BinaSortTree binaSortTree) { if (binaSortTree == null) { return; } Stack<BinaSortTree> treeStack = new Stack<BinaSortTree>(); treeStack.push(binaSortTree); while (!treeStack.isEmpty()) { BinaSortTree biTree = treeStack.peek(); if (biTree.lchild != null) { biTree.lchild = biTrees[biTree.lchild.value]; createTree(biTree.lchild); } if (biTree.rchild != null) { biTree.rchild = biTrees[biTree.rchild.value]; createTree(biTree.rchild); } treeStack.pop(); } } private static void printPath(BinaSortTree root,int sum,Stack<Integer> stack){ if(root != null){ sum = sum + valueArr[root.value]; stack.push(root.value); printPath(root.lchild, sum, stack); printPath(root.rchild, sum, stack); if(sum == k && root.lchild == null && root.rchild == null){ printfStack(stack); } stack.pop(); } } private static void printfStack(Stack<Integer> stack) { System.out.printf("A path is found: "); StringBuffer sb = new StringBuffer(); for(int num:stack){ sb.append(num+" "); } System.out.printf(sb.toString().trim()); System.out.println(); } private static class BinaSortTree { private BinaSortTree lchild; private BinaSortTree rchild; private int value ; public BinaSortTree(int value) { super(); this.value = value; } public BinaSortTree(BinaSortTree lchild, BinaSortTree rchild, int value) { super(); this.lchild = lchild; this.rchild = rchild; this.value = value; } } } /************************************************************** Problem: 1368 User: wzqwsrf Language: Java Result: Accepted Time:1310 ms Memory:63380 kb ****************************************************************/