Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 747453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:12:24+00:00 2026-05-14T14:12:24+00:00

I have worked on UVA 10410 Tree Reconstruction several days. But I can’t get

  • 0

I have worked on UVA 10410 Tree Reconstruction several days. But I can’t get the correct answer unitl now.

I have used an algorithm similar to the one which we always use to recovery a binary tree through the preorder traversal and the inorder traversal. But it can’t work.

Can anyone help me? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:12:24+00:00Added an answer on May 14, 2026 at 2:12 pm

    “Note that when a parent was expanded the children were traversed in
    ascending order.”

    This sentence is very important!

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    struct Node
    {
        int value;
        Node *child; //left child
        Node *sibling; //right sibling
    
        Node(int v)
        {
            value = v;
            child = NULL;
            sibling = NULL;
        }
    };
    
    void BuildTree(Node *&root,vector<int> bfs,vector<int> dfs)
    {
        root = NULL;
        if(bfs.size() == 1)
            root = new Node(bfs[0]);
        if(bfs.size() > 1)
        {
            root = new Node(bfs[0]);
    
            vector<int> candidate; //store candidate childs
            unsigned i;
            for(i = 1; i < bfs.size(); i++)
            {
                if(bfs[i] >= bfs[1])
                    candidate.push_back(bfs[i]);
                else
                    break;
            }
    
            //remove the non-candidate node
            int boundOfChild = candidate.size() - 1;
            for(i = candidate.size() - 1; i >= 1; i--)
            {
                vector<int>::iterator it1;
                vector<int>::iterator it2;
                it1 = find(dfs.begin(),dfs.end(),candidate[i]);
                it2 = find(dfs.begin(),dfs.end(),candidate[i - 1]);
                if(it1 < it2)
                    boundOfChild = i;
            }
            if(boundOfChild != candidate.size() - 1)
                candidate.erase(candidate.begin() + boundOfChild,candidate.end());
    
            //get every child's bfs and dfs
            for(i = candidate.size(); i >= 1; i--)
            {
                vector<int>::iterator it1;
                vector<int>::iterator it2;
                if(i == candidate.size())
                {
                    it1 = find(dfs.begin(),dfs.end(),candidate[i - 1]);
                    it2 = dfs.end();
                }
                else
                {
                    it1 = find(dfs.begin(),dfs.end(),candidate[i - 1]);
                    it2 = find(dfs.begin(),dfs.end(),candidate[i]);
                }
    
                vector<int> tmp_dfs(it1,it2);
                vector<int> tmp_bfs;
                for(vector<int>::iterator it = bfs.begin(); it < bfs.end(); it++)
                {
                    if(find(tmp_dfs.begin(),tmp_dfs.end(),*it) != tmp_dfs.end())
                        tmp_bfs.push_back(*it);
                }
    
                Node *tmp = root->child;
                BuildTree(root->child,tmp_bfs,tmp_dfs);
                root->child->sibling = tmp;
            }
        }
    }
    
    void PrintTree(Node *root)
    {
        if(root == NULL)
            return;
        queue<Node*> Q;
        Q.push(root);
        while(!Q.empty())
        {
            Node *tmp = Q.front();
            Q.pop();
            cout << tmp->value << ": ";
            tmp = tmp->child;
            while(tmp)
            {
                cout << tmp->value << ",";
                Q.push(tmp);
                tmp = tmp->sibling;
            }
            cout << endl;
        }
    }
    
    //just test case
    int BFS[] = {7,8,12,4,5,1,6,11,2,3,10,9,13,14};
    int DFS[] = {7,8,4,5,2,3,12,1,6,10,14,11,9,13};
    
    int main()
    {
        vector<int> vBFS(BFS,BFS + sizeof(BFS) / sizeof(int));
        vector<int> vDFS(DFS,DFS + sizeof(DFS) / sizeof(int));
    
        Node *root = NULL;
        BuildTree(root,vBFS,vDFS);    
        PrintTree(root);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have worked out about 90% of this, but can't seem to get the
I have worked with SQL for several years now, primarily MySQL/PhpMyAdmin, but also Oracle/iSqlPlus
I have worked on this problem for my entire day and can't solve it.
I have worked with pointers a lot, but still whenever I work with them,
I have worked many projects with no problems. But in current project, I tested
I have worked in a SOAP message to get the LoginToken from a Webservice,
I have worked with globalization settings in the past but not within the .NET
I have worked through the answer provided here . I have been able to
I have worked on several distributed client/server projects recently, and one pain point that
I have worked with code which had NUnit test written. But, I have never

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.