zoukankan      html  css  js  c++  java
  • 1020. Tree Traversals

    1020. Tree Traversals (25)

    时间限制
    400 ms
    内存限制
    32000 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

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

    Output Specification:

    For each test case, print in one line the level order traversal sequence of the corresponding binary tree. 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:
    7
    2 3 1 5 7 6 4
    1 2 3 4 5 6 7
    
    Sample Output:
    4 1 6 3 5 7 2
    
      1 #include <iostream>
    2 #include <fstream>
    3 #include <vector>
    4 #include <string>
    5 #include <algorithm>
    6 #include <map>
    7 #include <stack>
    8 #include <cmath>
    9 #include <queue>
    10 #include <set>
    11
    12
    13 using namespace std;
    14
    15 class Node
    16 {
    17 public:
    18 Node* left;
    19 Node* right;
    20 int value;
    21 };
    22
    23 Node* getNode( int inorder[30] , int postorder[30] , int i_base , int i_end, int p_base , int p_end )
    24 {
    25 if( i_end > i_base && p_end > p_base )
    26 {
    27 Node *center = new Node();
    28
    29 center->value = postorder[ p_end ];
    30
    31 int i_left_end = i_base;
    32
    33 for( ; i_left_end <= i_end ; ++i_left_end )
    34 {
    35 if( inorder[i_left_end] == center->value )
    36 {
    37 break;
    38 }
    39 }
    40
    41 --i_left_end;
    42
    43 if( i_left_end >= i_base )
    44 {
    45 int p_left_end = i_left_end - i_base + p_base;
    46
    47 center->left = getNode( inorder , postorder, i_base , i_left_end , p_base , p_left_end );
    48 }
    49 else
    50 {
    51 center->left = NULL;
    52 }
    53
    54 int i_right_base = i_left_end + 2;
    55
    56 if( i_right_base <= i_end )
    57 {
    58 int p_right_base = p_end - 1 - ( i_end - i_right_base );
    59
    60 center->right = getNode( inorder , postorder, i_right_base , i_end , p_right_base , p_end - 1 );
    61 }
    62 else
    63 {
    64 center->right = NULL;
    65 }
    66
    67 return center;
    68 }
    69 else if( i_end == i_base && p_end == p_base )
    70 {
    71 Node *center = new Node();
    72
    73 center->value = postorder[ p_end ];
    74
    75 center->left = NULL;
    76 center->right = NULL;
    77 return center;
    78 }
    79 else
    80 {
    81 return NULL;
    82 }
    83
    84
    85 }
    86
    87
    88 int main()
    89 {
    90
    91
    92 int N;
    93
    94 int p_order[30];
    95 int i_order[30];
    96
    97
    98 while( cin >> N )
    99 {
    100 for( int i = 0 ; i < N ; ++i )
    101 {
    102 cin >>p_order[i];
    103 }
    104
    105 for( int i = 0 ; i < N ; ++i )
    106 {
    107 cin >>i_order[i];
    108 }
    109
    110 Node* tree = getNode( i_order , p_order , 0 , N -1 , 0 , N - 1 );
    111
    112 queue<Node*> queue;
    113
    114 queue.push(tree);
    115
    116 bool first = true;
    117 while( !queue.empty() )
    118 {
    119 if(!first)
    120 {
    121 cout << " ";
    122 }
    123 else
    124 {
    125 first = false;
    126 }
    127
    128 cout << queue.front()->value;
    129
    130 if( queue.front()->left != NULL )
    131 {
    132 queue.push(queue.front()->left);
    133 }
    134
    135 if(queue.front()->right != NULL)
    136 {
    137 queue.push(queue.front()->right);
    138 }
    139
    140 queue.pop();
    141 }
    142 cout << endl;
    143
    144 }
    145
    146 return 0;
    147 }


  • 相关阅读:
    剑指17.树的子结构
    剑指16.合并两个排序的链表
    剑指15.反转链表
    剑指14.链表中倒数第k个结点
    剑指13.调整数组顺序使奇数位于偶数前面
    剑指12.数值的整数次方
    剑指11.二进制中1的个数
    剑指10.矩形覆盖
    剑指09.变态跳台阶
    JS 中动态创建json,动态为json添加属性、属性值
  • 原文地址:https://www.cnblogs.com/kking/p/2331815.html
Copyright © 2011-2022 走看看