版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BLueberry_Pie/article/details/84654443
数据结构实验之二叉树四:(先序中序)还原二叉树
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。
Input
输入数据有多组,每组数据第一行输入1个正整数N(1 <= N <= 50)为树中结点总数,随后2行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区分大小写)的字符串。
Output
输出一个整数,即该二叉树的高度。
Sample Input
9 ABDFGHIEC FDHGIBEAC
Sample Output
5
Hint
Source
xam
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
sc.in(System.in);
while (!sc.to.hasMoreTokens()) {
int n = sc.nextInt();
BT<Character> bt = new BT<>();
LinkedList<Character> Treea = new LinkedList<>();
LinkedList<Character> Treeb = new LinkedList<>();
String sa = sc.next();
String sb = sc.next();
char[] ca = sa.toCharArray();
char[] cb = sb.toCharArray();
for (int i = 0; i < ca.length; i++) {
Treea.add(ca[i]);
}
for (int i = 0; i < cb.length; i++) {
Treeb.add(cb[i]);
}
TN<Character> root = bt.CBPI(Treea, Treeb);
System.out.println(bt.Deep(root));
}
}
}
class BT<T> {
public TN<T> CBP(LinkedList<T> data) {
TN<T> root = null;
T d = data.removeFirst();
if (d != null) {
root = new TN<T>(d, null, null);
root.l = CBP(data);
root.r = CBP(data);
}
return root;
}
public TN<T> CBPI(LinkedList<T> pre, LinkedList<T> in) {
TN<T> root = new TN<T>(pre.getFirst(), null, null);
int i;
for ( i = 0; i < pre.size(); i++) {
if (root.d== in.get(i)) {
break;
}
}
if(i>0) {
LinkedList<T> preo = new LinkedList<>();
LinkedList<T> ino = new LinkedList<>();
for(int j=0;j<i;j++) {
preo.add(j,pre.get(j+1));
ino.add(j,in.get(j));
}
root.l=CBPI(preo,ino);
}else {
root.l=null;
}
if(pre.size()-i-1>0) {
LinkedList<T> preo = new LinkedList<>();
LinkedList<T> ino = new LinkedList<>();
for(int j=i+1;j<pre.size();j++) {
preo.add(j-i-1,pre.get(j));
ino.add(j-i-1,in.get(j));
}
root.r=CBPI(preo,ino);
}else {
root.r=null;
}
return root;
}
public void PreOrder(TN<T> root) {
if (root != null) {
System.out.print(root.d);
PreOrder(root.l);
PreOrder(root.r);
}
}
public void InOrder(TN<T> root) {
if (root != null) {
InOrder(root.l);
System.out.print(root.d);
InOrder(root.r);
}
}
public void PostOrder(TN<T> root) {
if (root != null) {
PostOrder(root.l);
PostOrder(root.r);
System.out.print(root.d);
}
}
public void LevelOrder(TN<T> root) {
if (root == null)
return;
LinkedList<TN> queue = new LinkedList<>();
TN<T> p;
queue.add(root);
while (!queue.isEmpty()) {
p = queue.removeFirst();
System.out.print(p.d);
if (p.l != null) {
queue.add(p.l);
}
if (p.r != null) {
queue.add(p.r);
}
}
}
public int Deep(TN<T> root) {
int d = 0;
if (root != null) {
int dl = Deep(root.l);
int dr = Deep(root.r);
d = (dl > dr ? dl : dr) + 1;
}
return d;
}
}
class TN<T> {
public T d;
public TN<T> l;
public TN<T> r;
public TN(T d, TN<T> l, TN<T> r) {
this.d = d;
this.l = l;
this.r = r;
}
}
class sc {
static BufferedReader re;
static StringTokenizer to;
static void in(InputStream in) {
re = new BufferedReader(new InputStreamReader(in), 32678);
to = new StringTokenizer("");
}
static String next() throws IOException {
while (!to.hasMoreTokens()) {
to = new StringTokenizer(re.readLine());
}
return to.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}