PAT-1020 Tree Traversals

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

Code

Traditional approach: build a binary tree
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;

typedef struct node
{
    
    
    node *left;
    node *right;
    int val;
    node(int x)
    {
    
    
        left = NULL;
        right = NULL;
        val = x;
    }
} node;

node *build_tree(int *post, int *in, int len)
{
    
    
    if (len == 0)
        return NULL;
    int index;
    // int len_post = sizeof(post)/sizeof(post[0]);
    node *root = new node(post[len - 1]); // 每个子树的根结点
    for (int i = 0; i < len; i++)
    {
    
    
        if (in[i] == root->val)
        {
    
    
            index = i;
            break;
        }
    }
    // 在中序序列的根结点的左边找左子树
    root->left = build_tree(post, in, index);
    // 在中序序列的根结点的右边找右子树
    // 后序序列要index,因为index个节点不会是右子树的
    root->right = build_tree(post + index, in + index + 1, len - index - 1);
    return root;
    //     7
    // 2 3 1 5 7 6 4 后序
    // 1 2 3 4 5 6 7 中序
}
void pre_visit(node *root)
{
    
    
    if (root == nullptr)
        return;
    cout << root->val << " ";
    pre_visit(root->left);
    pre_visit(root->right);
    return;
}
void eachfloor_visit(node *root)
{
    
    
    if (root == nullptr)
        return;
    queue<node *> q;
    cout << root->val;
    if (root->left)
        q.push(root->left);
    if (root->right)
        q.push(root->right);
    while (!q.empty())
    {
    
    
        node *temp = q.front();
        q.pop();
        cout << " " << temp->val;
        if (temp->left)
            q.push(temp->left);
        if (temp->right)
            q.push(temp->right);
    }
    return;
}
int main()
{
    
    
    int n;
    cin >> n;
    int *post = new int[n];
    int *in = new int[n];
    for (int i = 0; i < n; i++)
        cin >> post[i];
    for (int i = 0; i < n; i++)
        cin >> in[i];
    node *root = build_tree(post, in, n);
    eachfloor_visit(root);
    return 0;
}
Do not build a binary tree, and use index to indicate the output order of each node. If the root node subscript is i, then the subscript of the node of the left subtree is (2i+1), and the right subtree is (2i+2); use map Save the subscript and the val of the node, and finally output the map (for ideas, please refer to Liu Wei)

The idea of ​​dfs is to search in the range of the left subtree and the right subtree.

#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
vector<int> post, in;
map<int, int> res;

// 用下标记录节点的层级顺序,左(2i+1), 右边(2i+2),保存,最后排序输出
void dfs(int root, int begin, int end, int index)
{
    
    
    if (begin > end)
        return;
    int i = begin;
    while (i < end && post[root] != in[i])
        i++;
    res[index] = post[root];
    // 遍历左子树
    // root-(end-i)-1
    dfs(root - end - 1 + i, begin, i - 1, index * 2 + 1);
    dfs(root - 1, i + 1, end, index * 2 + 2);
}
int main()
{
    
    
    int n;
    cin >> n;
    post.resize(n);
    in.resize(n);
    for (int i = 0; i < n; i++)
        cin >> post[i];
    for (int i = 0; i < n; i++)
        cin >> in[i];
    dfs(n - 1, 0, n - 1, 1);
    map<int, int>::iterator it;
    // auto it = res.begin();
    it = res.begin();
    cout << it->second;
    // it++;
    // while(it !=res.end()){
    
    
    //     cout << " " <<it->second;
    //     it++;
    // }
    while (++it != res.end())
        cout << " " << it->second;
    return 0;
}

to sum up

  • vector reset the number size, vector.resize(n)
  • Map traversal: map<int, int>::iterator it;, it->second;

Guess you like

Origin blog.csdn.net/weixin_42100456/article/details/108832293