zoukankan      html  css  js  c++  java
  • 1127 ZigZagging on a Tree

    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.

    zigzag.jpg

    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

    题意:

      给出中序和后序遍历的结果,要求按照从右到左,从左到右依次替换的方法来输出层次遍历的结果。(Too native.)

    思路:

      构造数,层次遍历,遍历的过程中用双向队列来存储每一层的数据,没遍历完一层,根据要求判断所在层数是pop_front()还是pop_back()。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 vector<int> inOrder(50);
     6 vector<int> postOrder(50);
     7 
     8 typedef struct Node* node;
     9 
    10 struct Node {
    11     int date;
    12     node left;
    13     node right;
    14     Node(int v) {
    15         date = v;
    16         left = NULL;
    17         right = NULL;
    18     }
    19 };
    20 
    21 node buildTree(int index, int left, int right) {
    22     if (left > right) return NULL;
    23     node root = new Node(postOrder[index]);
    24     int pos;
    25     for (int i = left; i <= right; ++i) {
    26         if (inOrder[i] == postOrder[index]) {
    27             pos = i;
    28             break;
    29         }
    30     }
    31     int len = right - pos;
    32     root->left = buildTree(index - len - 1, left, pos - 1);
    33     root->right = buildTree(index - 1, pos + 1, right);
    34     return root;
    35 }
    36 
    37 void levelOrder(node root) {
    38     queue<node> que;
    39     deque<int> level;
    40     vector<int> ans;
    41     node last = new Node(-1);
    42     que.push(root);
    43     que.push(last);
    44     bool leftToRight = false;
    45     while (!que.empty()) {
    46         node temp = que.front();
    47         que.pop();
    48         if (temp->date == -1) {
    49             if (que.empty() && level.empty()) break;
    50             que.push(last);
    51             if (leftToRight) {
    52                 while (!level.empty()) {
    53                     ans.push_back(level.front());
    54                     level.pop_front();
    55                 }
    56                 leftToRight = false;
    57             } else {
    58                 while (!level.empty()) {
    59                     ans.push_back(level.back());
    60                     level.pop_back();
    61                 }
    62                 leftToRight = true;
    63             }
    64         } else {
    65             level.push_back(temp->date);
    66             if (temp->left) que.push(temp->left);
    67             if (temp->right) que.push(temp->right);
    68         }
    69     }
    70     cout << ans[0];
    71     for (int i = 1; i < ans.size(); ++i) cout << " " << ans[i];
    72     cout << endl;
    73 }
    74 
    75 int main() {
    76     int n;
    77     cin >> n;
    78 
    79     for (int i = 0; i < n; ++i) cin >> inOrder[i];
    80     for (int i = 0; i < n; ++i) cin >> postOrder[i];
    81 
    82     node root = buildTree(n - 1, 0, n - 1);
    83 
    84     levelOrder(root);
    85 
    86     return 0;
    87 }

      多看看别人的博客,看一下同一道问题的不同解,也能够让自己的思维更加的开阔。

      我写的代码还是按照中规中矩的方法来解决问题,看了柳神的博客感觉它的代码更加的简练,也更加的值得我去学习。

    Code:

     1 #include <iostream>
     2 #include <vector>
     3 #include <queue>
     4 using namespace std;
     5 vector<int> in, post, result[35];
     6 int n, tree[35][2], root;
     7 struct node {
     8     int index, depth;
     9 };
    10 void dfs(int &index, int inLeft, int inRight, int postLeft, int postRight) {
    11     if (inLeft > inRight) return;
    12     index = postRight;
    13     int i = 0;
    14     while (in[i] != post[postRight]) i++;
    15     dfs(tree[index][0], inLeft, i - 1, postLeft, postLeft + (i - inLeft) - 1);
    16     dfs(tree[index][1], i + 1, inRight, postLeft + (i - inLeft), postRight - 1);
    17 }
    18 void bfs() {
    19     queue<node> q;
    20     q.push(node{root, 0});
    21     while (!q.empty()) {
    22         node temp = q.front();
    23         q.pop();
    24         result[temp.depth].push_back(post[temp.index]);
    25         if (tree[temp.index][0] != 0)
    26             q.push(node{tree[temp.index][0], temp.depth + 1});
    27         if (tree[temp.index][1] != 0)
    28             q.push(node{tree[temp.index][1], temp.depth + 1});
    29     }
    30 }
    31 int main() {
    32     cin >> n;
    33     in.resize(n + 1), post.resize(n + 1);
    34     for (int i = 1; i <= n; i++) cin >> in[i];
    35     for (int i = 1; i <= n; i++) cin >> post[i];
    36     dfs(root, 1, n, 1, n);
    37     bfs();
    38     printf("%d", result[0][0]);
    39     for (int i = 1; i < 35; i++) {
    40         if (i % 2 == 1) {
    41             for (int j = 0; j < result[i].size(); j++)
    42                 printf(" %d", result[i][j]);
    43         } else {
    44             for (int j = result[i].size() - 1; j >= 0; j--)
    45                 printf(" %d", result[i][j]);
    46         }
    47     }
    48     return 0;
    49 }

    2020-07-07 21:29:02

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    查看网页源代码的方法
    Chrome浏览器清除缓存
    copy与内存管理
    分类(Category)
    @class的使用
    @property参数的
    力扣131题、93题(分割回文串,复原IP地址)
    力扣17题(电话号码的字母组合)
    力扣216(组合总和)
    力扣242题、383题(有效的字母异位词,赎金信)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12751174.html
Copyright © 2011-2022 走看看