JAVA数据结构实验之二叉树八:(中序后序)求二叉树的深度

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BLueberry_Pie/article/details/84654801

数据结构实验之二叉树八:(中序后序)求二叉树的深度

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

已知一颗二叉树的中序遍历序列和后序遍历序列,求二叉树的深度。

Input

输入数据有多组,输入T,代表有T组数据。每组数据包括两个长度小于50的字符串,第一个字符串表示二叉树的中序遍历,第二个表示二叉树的后序遍历。

Output

输出二叉树的深度。

Sample Input

2
dbgeafc
dgebfca
lnixu
linux

Sample Output

4
3

Hint

Source

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 t=sc.nextInt();
		while(t-->0) {
			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.CBIP(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 TN<T> CBIP(LinkedList<T> in, LinkedList<T> pos) {
		TN<T> root = new TN<T>(pos.getLast(), null, null);
		int i;
		for ( i = 0; i < pos.size(); i++) {
			if (root.d== in.get(i)) {
				break;
			}
		}
		
		if(i>0) {
			LinkedList<T> poso = new LinkedList<>();
			LinkedList<T> ino = new LinkedList<>();
			for(int j=0;j<i;j++) {
				poso.add(j,pos.get(j));
				ino.add(j,in.get(j));
			}
			root.l=CBIP(ino,poso);
		}else {
			root.l=null;
		}
		if(pos.size()-i-1>0) {
			LinkedList<T> poso = new LinkedList<>();
			LinkedList<T> ino = new LinkedList<>();
			for(int j=i+1;j<pos.size();j++) {
				poso.add(j-i-1,pos.get(j-1));
				ino.add(j-i-1,in.get(j));
				
			}
			root.r=CBIP(ino,poso);
		}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());
	}
}

猜你喜欢

转载自blog.csdn.net/BLueberry_Pie/article/details/84654801