[二叉树建树]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 traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

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 preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line "Yes" if the tree is unique, or "No" if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. 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 1:

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

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4

分析:目前,关于二叉树的遍历算法,有前序和中序,后序和中序,层序和中序,这三种组合都是可以唯一确定一颗二叉树的,目前都有用代码实现。其中,前序和中序建树和后序和中序建树两种方法都是想办法分出左右子树,然后对左右字数进行递归建树。层序和中序也可以用递归实现,但是目前我只用了非递归的实现方法。关于今天的建树方法,使用的是二叉树的前序遍历和后序遍历。但是我们知道,仅有前序遍历和后序遍历是没法确定一颗二叉树的。这里,前序遍历和后序遍历在某些调剂下可以唯一确定一颗二叉树。但这里不做这个要求。题目的要求是当产生歧义是可随意建立一颗满足前序和后序遍历的二叉树即可。

  目前已经碰到了二叉树建树的大多数情况,下次有时间稍微总结一下。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

vector<int > pre,post,in;

struct Node
{
    int data;
    Node *l,*r;
};

bool flag=true;

void creat(Node * & root,int preL,int preR,int postL,int postR)
{
    if(preL==preR)
    {
        root=new Node;
        root->data=pre[preL];
        root->l=root->r=NULL;
        return ;
    }
    root=new Node;
    root->data=pre[preL];
    root->l=root->r=NULL;
    int i,j;
    for(i=postL;i<=postR;i++)
    {
        if(post[i]==pre[preL+1])
        {
            break;
        }
    }
    int leftNum=i-postL;
    if(post[i]==post[postR-1])//不确定的条件,无法区分是左子树还是右子树
    {
        flag=0;
        creat(root->l,preL+1,preR,postL,postR-1);
    }
    else
    {
        creat(root->l,preL+1,preL+1+leftNum,postL,postL+leftNum);
        creat(root->r,preL+leftNum+2,preR,postL+leftNum+1,postR-1);
    }
}

vector<int> ans;

void inOrder(Node * root)
{
    if(root!=NULL)
    {
        inOrder(root->l);
        ans.push_back(root->data);
        inOrder(root->r);
    }
}

void outans()
{
    for(int i=0;i<ans.size();i++)
    {
        if(i>0) printf(" ");
        printf("%d",ans[i]);
    }
    printf("\n");
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int k;
        scanf("%d",&k);
        pre.push_back(k);
    }
    for(int i=0;i<n;i++)
    {
        int k;
        scanf("%d",&k);
        post.push_back(k);
    }
    Node * root=NULL;
    creat(root,0,n-1,0,n-1);
    inOrder(root);
    if(flag==true) printf("Yes\n");
    else printf("No\n");
    outans();
    return 0;
}
时间: 2024-08-05 19:31:31

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

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 / 9 20

笔试算法题(36):寻找一棵二叉树中最远节点的距离 &amp; 根据二叉树的前序和后序遍历重建二叉树

出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所以计算出以每个节点为根节点的子树的最 远距离,最后取他们的最大值就是整棵树的最远距离: 如果递归层次过多造成系统栈溢出,则可以使用stack堆栈结构存储递归节点,从而使用循环实现 解题: 1 struct Node { 2 int value; 3 Node *left; 4 Node *right

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见我之前的一篇博客Convert Sorted Array to Bin

[LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树

Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals?pre?and?post?are distinct?positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7] Note: 1 <=

889. 根据前序和后序遍历构造二叉树(非递归)

题目连接: https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题目大意: 中文题目 AC代码: 1 TreeNode * construcuFormPrePost(vrctor<int>& pre, vector<int>&post){ 2 3 if(pre.empty()) 4 return NULL; 5 stack<T

[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 这道题要求用先序和中序遍历来建立二叉树,跟之前那道Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树原理基本相同,针对这道题,由于先

根据先序遍历和中序遍历建立二叉树

title: 根据先序遍历和中序遍历建立二叉树 date: 2019-07-23 22:37:34 tags: 数据结构 问题 已知一棵二叉树的先序遍历以及中序遍历,重建二叉树.二叉树的每一个节点有三个属性,左子节点,右子节点,以及节点值. 思路 先序遍历服从规则“根左右”,所以由此可知,对于一个先序遍历得到的数组,第一个元素一定是根节点: 中序遍历服从规则”左根右“,所以由此可知,对于一个中序遍历得到的数组,根节点左边的元素都属于根节点的左子树,而根节点右边的元素都属于根节点的右子树: 所以,

java 二叉树的遍历 为什么只给出前序以及后序遍历,不能生成唯一的二叉树

最近在学习java的数据结构与算法知识,看到数据结构 树的遍历的方式.在理解过程中.查看到一篇文章,视野非常有深度,在信息论的角度看待这个问题.在此贴出该文章的链接以及内容. [文章出处]http://www.binarythink.net/2012/12/binary-tree-info-theory/ 我们在学习二叉树的遍历时,都会不可避免的学到二叉树的三种遍历方式,分别是遵循(根-左-右)的前序遍历.遵循(左-根-右)的中序遍历以及遵循(左-右-根)的后序遍历.并且每一个二叉树都可以用这三

Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树

题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是root=1,然后我们在中序遍历中搜索1,可以看到中序遍历的第四个数是1,也就是root.根据中序遍历的定义,1左边的数{4,2,5}就是左子树的中序遍历,1右边的数{6,3,7}就是右子树的中序遍历