[ALGO-20] Order first

Source: Internet
Author: User

Algorithm training requires a time limit of sorting in the first order: 1.0 s memory limit: MB. The problem description shows the middle and back order of a binary tree. Find its first order. (The agreed Tree node is represented by different uppercase letters, and the length is <= 8 ). The input format is two rows. Each row has one string, indicating a string in the middle and back order. The output format is one string, indicating the first order. The sample input is BADC.
BDCA sample output ABCD analysis: the binary tree simply knows the question of "Middle Order" and "back order" for "first order", and recursively solves the problem

Import java. util. extends; public class Main {public static void main (String [] args) {extends = new loads (System. in); while (partial. hasNext () {String str1 = substring. nextLine (); String str2 = bytes. nextLine (); showResult (str1, str2); System. out. println () ;}} private static void showResult (String str1, String str2) {char chlorophyll = str2.charAt (str2.length ()-1); System. out. print (chlorophyll); int index = str1.indexOf (chlorophyll); if (index> 0) {showResult (str1.substring (0, index), str2.substring (0, index ));} if (index <str1.length ()-1) {showResult (str1.substring (index + 1), str2.substring (index, str2.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.