1127 ZigZagging on a Tree (30 分)树的层次遍历

1127 ZigZagging on a Tree (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. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

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 inorder 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, print the zigzagging sequence of the tree in a line. 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:

8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1

Sample Output:

1 11 5 8 17 12 20 15思路:  我是先根据后序遍历和中序遍历序列建立这个二叉树使用的是二叉链表,然后进行层序遍历。因为题目要求进行“Z"字形层次遍历,所以不能在遍历中直接输出,而应该每遍历完一层再输出,比如第一层是顺序输出,第二次就逆序输出。那么怎么知道一层遍历结束了呢?使用last变量指向每层最后一个元素的下标,当front等于last的时候,一层就结束了。这种方法在求二叉树的宽度的时候也有用到。
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<set>
using namespace std;
bool flag=true;
vector<int> path;
//对树进行深度遍历

struct Node
{
    int data;
    Node *lchild,*rchild;
};
//Node *tree=nullptr;
void create(int in[],int il,int ir,int po[],int pl,int pr,Node* &root)
{
    if(root==nullptr)
    {
        root=new Node;
        root->data=po[pr];
        root->lchild=root->rchild=nullptr;
    }
    int i=0;
    while(in[i]!=po[pr]) i++;
    int len=i-il;//减去中心点坐标才对
    int rlen=ir-i;
    if(len>0)
        create(in,il,il+len-1,po,pl,pl+len-1,root->lchild);
    if(rlen>0)
        create(in,il+len+1,ir,po,pl+len,pr-1,root->rchild);

}

void print(Node *tree)
{
    if(tree)
    {
        print(tree->lchild);
        cout<<tree->data<<endl;
        print(tree->rchild);
    }
}

void level(Node *tree,int n)
{
    vector<int> print;
    int front=0;
    int rail=0;
    Node* queue[n+10];
    int odd=1;
    queue[rail++]=tree;
    cout<<tree->data;
    //print.push_back(tree->data);
    //front++;
    int last=1;
    while(front<rail)
    {
        //cout<<print.size()<<endl;
        Node *temp=queue[front++];
        //cout<<temp->data<<endl;
        //rail++;

        if(temp->lchild!=nullptr)
        {
            queue[rail++]=temp->lchild;
            if(temp->lchild)
                print.push_back(temp->lchild->data);
        }

        if(temp->rchild!=nullptr)
        {
            queue[rail++]=temp->rchild;
            if(temp->rchild)
                print.push_back(temp->rchild->data);
        }
        if(front==last)
        {
            last=rail;
            if(odd==2)
            {
                reverse(print.begin(),print.end());
                odd=1;
            }
            else
                odd=2;

            for(auto num:print)
                cout<<" "<<num;
            print.clear();
        }
    }
}

int main()
{
    int n;
    cin>>n;
    int in[n];
    int po[n];
    for(int i=0;i<n;i++)
        cin>>in[i];
    for(int j=0;j<n;j++)
        cin>>po[j];
    Node* tree=nullptr;
    create(in,0,n-1,po,0,n-1,tree);
    //print(tree);
    level(tree,n);
    return 0;
}
  

原文地址:https://www.cnblogs.com/zhanghaijie/p/10303188.html

时间: 2024-10-08 13:46:35

1127 ZigZagging on a Tree (30 分)树的层次遍历的相关文章

1127 ZigZagging on a Tree (30 分)

1127 ZigZagging on a Tree (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. And it is a simple standard routine to pr

PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树

根据中序遍历和前序遍历确定一棵二叉树,然后按"层次遍历"序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <string> #include <map> #define LEFT 0 #define

PAT 1127 ZigZagging on a Tree (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. And it is a simple standard routine to print the numbers in level-order. H

PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水

由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍历的水题... #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <queue> using namespace std;

紫书例题6-7 树的层次遍历

纯小白也能看懂的代码,一起努力 6.3.2 二叉树的层次遍历 例题6-7 树的层次遍历(Trees on the level, Duke 1993, UVa 122) 输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左括号和右括号之间没有空格,相邻结点之间用一个空格隔开.每棵树的输入用一对空括号"()"结束(这对括号本身不代表一个结点),如图6-3所示 注意,如果从根到某个叶结点的

数据结构:树的BFS,树的层次遍历! 按先序遍历创建一棵树,然后以层次遍历输出。

按先序遍历创建一棵树,以层次遍历输出 样例输入 A B # D # # C E # # F # # 样例输出 LevelOrder: A B C D E F 代码: #include <iostream> #include <queue> using namespace std; struct node { //表示一个树上的节点 char ch; node *left, *right; }; node* creat() { //以递归的方式构造一棵二叉树 node *root =

【PAT甲级】1099 Build A Binary Search Tree (30 分)

题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 pair<int,int>a[107]; 5 int b[107]; 6 int cnt=0; 7 int ans[107]

树——通用树的层次遍历

1,为何及如何按层次遍历通用树中的每一个数据元素? 1,通用树结构是一种容器类型的树结构,其用来装数据元素,所以应该提供一种方法来遍历树中的每一个数据结构: 2,往下分析: 2,当前的事实: 1,树是非线性的数据结构,树的结点没有固定的编号方式: 1,也就不能像链表一样统一编号来访问: 3,新的需求: 1,为通用树结构提供新的方法,快速遍历每一个结点: 4,设计思路(游标): 1,在树中定义一个游标(GTreeNode<T>*): 2,遍历开始前将游标指向根结点(root()): 3,获取游标

树的层次遍历(Java代码实现)

与树的前中后序遍历的DFS思想不同,层次遍历用到的是BFS思想.一般DFS用递归去实现(也可以用栈实现),BFS需要用队列去实现. 层次遍历的步骤是: 1.对于不为空的结点,先把该结点加入到队列中 2.从队中拿出结点,如果该结点的左右结点不为空,就分别把左右结点加入到队列中 3.重复以上操作直到队列为空 1 public class Solution{ 2 class TreeNode { 3 int val; 4 TreeNode left; 5 TreeNode right; 6 TreeN