参考代码:
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语言代码)浏览:875 |
【回文数(二)】 (C++代码)浏览:873 |
汽水瓶 (C语言代码)浏览:608 |
Tom数 (C++代码)浏览:828 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1428 |
Cylinder (C语言描述+详细分析)浏览:3265 |
矩阵的对角线之和 (C语言代码)浏览:1356 |
矩阵转置 (C语言代码)浏览:783 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:470 |
买不到的数目 (C语言代码)浏览:3094 |