参考代码:
import java.util.*; public class Main { static char[] preorder; static char[] inorder; class Node{ char val; Node left; Node right; Node(char val) { this.val = val; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { preorder = scanner.nextLine().toCharArray(); inorder = scanner.nextLine().toCharArray(); Node root = build(0, preorder.length - 1, 0, inorder.length - 1); print(root); System.out.println(); } } public static void print(Node root) { if(root == null) return; if(root.left != null) print(root.left); if(root.right != null) print(root.right); System.out.print(root.val); } public static Node build(int l1, int r1, int l2, int r2) { if(l1 > r1) return null; Node root = new Main().new Node(preorder[l1]); int mid = l2; while(inorder[mid] != root.val) mid++; root.left = build(l1 + 1, mid - l2 + l1, l2, mid - 1); root.right = build(mid - l2 + l1 + 1, r1, mid + 1, r2); return root; } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:1535 |
矩形面积交 (Java代码)浏览:1223 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:674 |
C语言程序设计教程(第三版)课后习题1.5 (C++代码)浏览:1095 |
C语言程序设计教程(第三版)课后习题6.3 (C++代码)浏览:963 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1434 |
C语言训练-数字母 (C语言代码)浏览:608 |
K-进制数 (C语言描述,蓝桥杯)浏览:925 |
核桃的数量 (C语言代码)浏览:872 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:671 |