03-树3 Tree Traversals Again

文章描述了一种非递归方法来实现二叉树的中序遍历,给定一系列基于栈的操作,可以生成一棵唯一的二叉树。然后要求给出该树的后序遍历序列。程序通过输入的中序遍历操作序列构建二叉树,并输出后序遍历的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

Sample Output:

3 4 2 6 5 1

解答

#include <iostream>
#include <stack>
#include <string>
using namespace std;

template <typename T>
class Node
{
public:
    T data;
    Node<T> *left;
    Node<T> *right;
    Node(int d = 0, Node<T> *p = nullptr, Node<T> *q = nullptr)
    {
        data = d;
        left = p;
        right = q;
    }
};

template <typename T>
class Tree
{
public:
    unsigned N;
    Node<T> *root;
    Tree(int n) : N(n)
    {
        if (n == 0)
        {
            root = nullptr;
            return;
        }
        string op;
        int d;
        stack<Node<T> *> s;
        Node<T> *t = nullptr;
        for (int i = 0; i < 2 * n; ++i)
        {
            cin >> op;
            if (op == "Push")
            {
                cin >> d;
                Node<T> *p = new Node<T>(d);
                if (i == 0)
                    root = p;
                if (t and !t->right)
                    t->right = p;
                else if (!s.empty())
                    s.top()->left = p;
                s.push(p);
            }
            else if (op == "Pop")
            {
                t = s.top();
                s.pop();
            }
            else
                exit(-1);
        }
    }
    void postOrder(Node<T> *p)
    {
        if (!p)
            return;
        postOrder(p->left);
        postOrder(p->right);
        if (p != root)
            cout << p->data << " ";
    }
    ~Tree()
    {
        ;
    }
};

int main()
{
    int n = 0;
    cin >> n;
    Tree<unsigned> t1(n);
    t1.postOrder(t1.root);
    cout << t1.root->data;
    cout << endl;
    return 0;
}
#include <iostream>
#include <string>
#include <stack>
/*
using namespace std;

template <typename T>
class Node
{
public:
    T data;
    Node<T> *left;
    Node<T> *right;
    Node(int d = 0, Node<T> *p = nullptr, Node<T> *q = nullptr)
    {
        data = d;
        left = p;
        right = q;
    }
};

template <typename T>
class Tree
{
public:
    unsigned N;
    Node<T> *root;
    Tree(int n) : N(n)
    {
        if (n == 0)
        {
            root = nullptr;
            return;
        }
        string op;
        int d;
        stack<Node<T> *> s;
        Node<T> *t = nullptr;
        for (int i = 0; i < 2 * n; ++i)
        {
            cin >> op;
            if (op == "Push")
            {
                cin >> d;
                Node<T> *p = new Node<T>(d);
                if (i == 0)
                    root = p;
                if (t and !t->right)
                    t->right = p;
                else if (!s.empty())
                    s.top()->left = p;
                s.push(p);
            }
            else if (op == "Pop")
            {
                t = s.top();
                s.pop();
            }
            else
                exit(-1);
        }
    }
    void postOrder(Node<T> *p)
    {
        if (!p)
            return;
        postOrder(p->left);
        postOrder(p->right);
        if (p != root)
            cout << p->data << " ";
    }
    ~Tree()
    {
        ;
    }
};

int main()
{
    int n = 0;
    cin >> n;
    Tree<unsigned> t1(n);
    t1.postOrder(t1.root);
    cout << t1.root->data;
    cout << endl;
    return 0;
}
*/
class Solution
{
private:
    int _n;
    int *preorder;
    int *inorder;
public:
    int *postorder;

    Solution(int n, const int *pre, const int *in) : _n(n)
    {
        preorder = new int[n];
        inorder = new int[n];
        postorder = new int[n];
        for (int i = 0; i < n; ++i)
        {
            preorder[i] = pre[i];
            inorder[i] = in[i];
        }
    }

    void solve(int preL, int inL, int postL, int n)
    {
        if (n == 0) return;
        if (n == 1)
        {
            postorder[postL] = preorder[preL];
            return;
        }
        int root = preorder[preL];
        postorder[postL + n - 1] = root;
        int i = 0;
        for (; i < n; ++i)
            if (inorder[inL + i] == root)
                break;
        solve(preL + 1, inL, postL, i);
        solve(preL + i + 1, inL + i + 1, postL + i, n - i - 1);
    }
    
    void print()
    {
        int i = 0;
        for(;i<_n-1;++i)
            std::cout<<postorder[i]<<' ';
        std::cout<<postorder[i]<<'\n';
    }

    ~Solution()
    {
        delete[]preorder;
        delete[]inorder;
        delete[]postorder;
    }
};

int main()
{
    int n = 0, d= 0;
    std::string op;
    std::stack<int> s;
    std::cin>>n;
    int *pre = new int[n];
    int *in = new int[n];
    for (int i = 0,j=0,k=0; i < 2 * n; ++i)
    {
        std::cin >> op;
        if (op == "Push")
        {
            std::cin >> d;
            pre[j++]=d;
            s.push(d);
        }
        else
        {
            d = s.top();
            in[k++] = d;
            s.pop();
        }
    }
    Solution S(n, pre, in);
    S.solve(0, 0, 0, n);
    S.print();
    delete []pre;
    delete []in;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值