18.11.02 rebuilding a binary tree from the root sequence and the back root sequence-Data Structure exercises

Source: Internet
Author: User

Subject content:

We know how to travel around a binary tree in Three depth priorities to get the root sequence, the front root sequence, and the back root sequence. In turn, if the root sequence and root sequence of a given binary tree are given, or the root sequence and root sequence are given, a binary tree can be rebuilt. In this question, we enter the root sequence and the back root sequence of a binary tree. We need to re-build the binary tree in the memory, and finally output the front root sequence of the binary tree.

Different integers are used to uniquely identify each node of a binary tree. The following Binary Tree

 

The root sequence is 9 5 32 67

Posterior Root sequence 9 32 67 5

Anterior root sequence 5 9 67 32

 

 

 

Input Format:

Two rows. The first row is the root sequence of the binary tree, and the second row is the back root sequence. Each number represents a node separated by a space. Node number range: 0 ~ 65535. You do not have to consider improper data input.

 

Output Format:

One row. The front root sequence of the binary tree reconstructed from the root sequence and the back root sequence in the input. Each number represents a node separated by a space.

 

Input example:

9 5 32 679 32 67 5

 

Output example:

5 9 67 32

 

Time Limit: Ms memory limit: KB
1 # include <iostream> 2 # include <string. h> 3 # include <algorithm> 4 # include <stack> 5 # include <string> 6 # include <math. h> 7 # include <queue> 8 # include <stdio. h> 9 # include <string. h> 10 # include <vector> 11 # include <fstream> 12 # include <set> 13 14 using namespace STD; 15 int mid [65536], back [65536]; 16 int count0 = 0; // number of nodes 17 bool flag; // whether the first output is 18 19 void Init () {20 int I = 0; 21 scanf ("% d", & Mi D [++ I]); 22 while (getchar ()! = '\ N') // when a line break is read, it means that a row has read 23 scanf ("% d", & Mid [++ I]); 24 count0 = I; 25 For (INT I = 1; I <= count0; I ++) 26 scanf ("% d", & back [I]); 27} 28 29 void Front (INT Mids, int mide, int backs, int Backe) {30 if (MIDS> mide) return; 31 if (FLAG) {32 printf ("% d", back [Backe]); 33 flag = false; 34} 35 else36 printf ("% d", back [Backe]); 37 int Spot = find (Mid + Mids, Mid + mide + 1, back [Backe])-mid; 38 int LFT = spot-mids; 39 Front (MIDS, spot-1, backs, backs + LFT-1); // recursively output left subtree root node 40 Front (spot + 1, mide, backs + LFT, backe-1 ); // recursively output the right subtree's root node 41} 42 43 int main () 44 {45 flag = true; 46 Init (); 47 Front (1, count0, 1, count0); 48 return 0; 49}
View code

It's too difficult to answer questions today

At the beginning, I thought that the data of this oj was very watery based on previous experience, so we should use recursion directly.

Actually TLE

Then change Stack

Still TLE ??

It seems that there is a problem in the input, but I still don't know why, that is, I used the originally sorted array to judge the input with getchar, and changed it to the number of elements input, then it's over.

Isn't there a line break in the question data ???

Today, it seems that you don't have to do the problem. It's better to go home and play the game.

18.11.02 rebuilding a binary tree from the root sequence and the back root sequence-Data Structure exercises

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.