Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么我的代码中显示runtime Errorimport java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main{ class Node { char data; Node leftNode; Node rightNode; public char getData() { return data; } public void setData(char data) { this.data = data; } public Node getLeftNode() { return leftNode; } public void setLeftNode(Node leftNode) { this.leftNode = leftNode; } public Node getRightNode() { return rightNode; } public void setRightNode(Node rightNode) { this.rightNode = rightNode; } } public static Node constructTree(List<Character> pre, List<Character> mid, int length) { Main mainA2255 = new Main(); Node node = mainA2255.new Node(); if (length == 0) return null; int rootIndex = 0; for (int i = 0; i < length; i++) { if (mid.get(i) == pre.get(0)) { rootIndex = i; break; } } node.data = mid.get(rootIndex); node.leftNode = constructTree(pre.subList(1, pre.size()), mid, rootIndex); node.rightNode = constructTree(pre.subList(rootIndex + 1, pre.size()), mid.subList(rootIndex + 1, mid.size()), length - rootIndex - 1); return node; } public static void postOrder(Node node) { if (node == null) return; postOrder(node.leftNode); postOrder(node.rightNode); System.out.print(node.data); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { String string = scanner.nextLine(); String[] strs = string.split(" "); if (strs.length >= 2) { char[] pre = strs[0].toCharArray(); char[] mid = strs[1].toCharArray(); if (pre.length == mid.length) { ArrayList<Character> pre1 = new ArrayList<Character>(); ArrayList<Character> mid1 = new ArrayList<Character>(); for (int i = 0; i < pre.length; i++) { pre1.add(pre[i]); mid1.add(mid[i]); } postOrder(constructTree(pre1, mid1, pre1.size())); } } } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator