PAT A 1119. Pre- and Post-order Traversals (30)【二叉树遍历】

No.1119

题目:由前序后序二叉树序列,推中序,判断是否唯一后输出一组中序序列

思路:前序从前向后找,后序从后向前找,观察正反样例可知,前后序树不唯一在于单一子树是否为左右子树。

判断特征:通过查找后序序列中最后一个结点的前一个在先序中的位置,来确定是否可以划分左右孩子,如果不能,  就将其划分为右孩子(或左孩子),递归建树。

中序遍历输出。

#include <iostream>
using namespace std;
const int maxn = 31;

int n, index = 0;
int pre[maxn], post[maxn];
bool flag = true;

struct Node {
    int data;
    Node *lchild, *rchild;
} *root;

Node *create(int preL, int preR, int postL, int postR)
{
    if (preL > preR) return NULL;
    Node *node = new Node;
    node->data = pre[preL];
    node->lchild = NULL;
    node->rchild = NULL;
    if (preL == preR)
        return node;
    int k = 0;
    for (k = preL + 1; k <= preR; k++)
    {
        if (pre[k] == post[postR - 1]) break;
    }
    if (k - preL > 1)
    {
        node->lchild = create(preL + 1, k - 1, postL, postL + k - preL - 2);
        node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
    }
    else
    {
        flag = false;
        node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
    }
    return node;
}

void inOrder(Node *node)
{
    if (node == NULL) return;
    inOrder(node->lchild);
    if (index < n - 1)
        cout << node->data << " ";
    else cout << node->data << endl;
    index++;
    inOrder(node->rchild);
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> pre[i];
    for (int i = 0; i < n; ++i) cin >> post[i];
    root = create(0, n - 1, 0, n - 1);
    if (flag) cout << "Yes\n";
    else cout << "No\n";
    inOrder(root);
    return 0;
}
时间: 2024-10-02 05:06:50

PAT A 1119. Pre- and Post-order Traversals (30)【二叉树遍历】的相关文章

PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子.中序遍历的顺序正好是序列从小到大的顺序,因此中序遍历的时候顺便赋值就可以了,最后层次遍历输出. 思路一:中序遍历的时候赋值 #include <iostream> #include <cstdio> #include <algorithm> #include <str

[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)

1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversa

1119 Pre- and Post-order Traversals (30 分)

1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder travers

Construct a tree from Inorder and Level order traversals

Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is an example to illustrate the problem. BinaryTree Input: Two arrays that represent Inorder and level order traversals of a Binary Treein[]    = {4, 8, 1

【LeetCode-面试算法经典-Java实现】【103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)】

[103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alt

【LeetCode-面试算法经典-Java实现】【102-Binary Tree Level Order Traversal(二叉树层序遍历)】

[102-Binary Tree Level Order Traversal(二叉树层序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15

hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

Binary Tree Traversals Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called

PAT甲题题解-1119. Pre- and Post-order Traversals (30)-(根据前序、后序求中序)

(先说一句,题目还不错,很值得动手思考并且去实现.) 题意:根据前序遍历和后序遍历建树,输出中序遍历序列,序列可能不唯一,输出其中一个即可. 已知前序遍历和后序遍历序列,是无法确定一棵二叉树的,原因在于如果只有一棵子树可能是左孩子也有可能是右孩子.由于只要输出其中一个方案,所以假定为左孩子即可.下面就是如何根据前序和后序划分出根节点和左右孩子,这里需要定义前序和后序的区间范围,分别为[preL,preR],[postL,postR]. 一开始区间都为[1,n],可以发现前序的第一个和后序的最后一

PAT A1086 Tree Traversals Again [二叉树前序中序求后序]

题目描述 链接 用栈的形式给出一棵二叉树的建立的顺序,求这棵二叉树的后序遍历 分析 性质:树的先序等于入栈次序,树的中序遍历等于出栈次序 先序:先访问根再入栈,所以入栈次序就是先序遍历次序 中序:先递归访问左子树,回溯时访问根,回溯时即出栈时,所以出栈次序就是中序遍历 所以问题转换为已知先序中序,求后序 已知先序中序求后序的方法 \(root\) 保存先序中根的位置,\(st\),\(ed\) 为中序子树的起始结束位置 遍历中序,找到中序根的位置\(i\),从而分成左右子树 左子树在先序中根的位