In binary tree, we know the sequence of middle order and sequential order, first order sequence.

Source: Internet
Author: User
/* Problem description gives a binary tree in the order and sequence. Find out its first order arrangement.
(The Convention tree node is represented in different capitals, the length <=8).
Input format two lines, one string per line, respectively, the middle order and the sequence of output format a string, representing the first order of the sample input BADC BDCA sample output abcd*/import java.util.*;
	public class main{public static String x,y,result= "";
		public static void Main (string[] args) {Scanner sc=new Scanner (system.in);
		X=sc.next ();
		Y=sc.next (); 
		Xianxu (X,y);
	SYSTEM.OUT.PRINTLN (result); }//recursive traversal of binary tree sequence and subsequent public static void Xianxu (String center,string last) {if (Center.length () ==0 && last.length	
		() ==0) {return;
				else{//The last word of the sequence Poute int length=last.length ();
				The last character of the sequence, that is, the root node String ls=last.substring (Last.length ()-1);	
				The first sequence, which is the root node result+=ls, is transformed into a first order;   
				The root node is subscript int index=center.indexof (LS) at the center of the sequence;
				if (index>0) {//recursively traverses and transforms the left subtree of the root node Xianxu (center.substring (0,index), last.substring (0,index)); } if (length>1) {//recursively traverse and convert the right subtree of the root node Xianxu (center.substring (index+1,length), last.substring (index,length-1))
				; }
		}
	}
}

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.