UVA 536 Tree Recovery (two-pronged reconstruction) __uva

Source: Internet
Author: User

Sequential traversal based on first-order traversal and middle-order traversal

import Java.util.Scanner;
		public class Main {public static void main (string[] args) {Scanner scan = new Scanner (system.in);
			while (Scan.hasnext ()) {String pre = Scan.next ();
			String mid = Scan.next ();
			Node root = Build (Pre,mid,0,pre.length () -1,0,mid.length ()-1);
			DFS (root);
		System.out.println ();
			} public static void Dfs (Node root) {if (root!=null) {if (root.left!=null) {DFS (root.left);
			} if (Root.right!=null) {DFS (root.right);
		} System.out.print (ROOT.C); } public static Node build (String pre,string mid,int pre1,int pre2,int mid1,int mid2) {if pre1>pre2| |
		MID1>MID2) return null;
		char C = Pre.charat (Pre1);
		Node root = new node ();
		ROOT.C = C;
		int p = 0;
		while (Mid.charat (p)!=c) p++;
		int cnt = P-MID1;
		Root.left = Build (pre,mid,pre1+1,pre1+cnt,mid1,p-1);
		Root.right = Build (PRE,MID,PRE1+CNT+1,PRE2,P+1,MID2);
	return root;
		Static class node{char C;
	Node Left,right; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.