zoukankan      html  css  js  c++  java
  • 重建二叉树

    题目描述:

    输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。

    假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列,如果如果题目中所给的前序和中序遍历序列不能构成一棵二叉树,则输出”No”。

    算法:

     1 #include<iostream>
     2 
     3 using namespace std;
     4 int tags = 0;
     5 
     6 typedef struct BinaryTreeNode{              //二叉树的结构体
     7     int data;
     8     struct BinaryTreeNode *left;
     9     struct BinaryTreeNode *right;
    10 }BinaryTreeNode; 
    11 
    12 BinaryTreeNode* construct(int *startPreorder, int *endPreorder, int *startInorder, int *endInorder){             //核心算法
    13     
    14     int rootValue = startPreorder[0];
    15     BinaryTreeNode *root =  new BinaryTreeNode();
    16     root->data = rootValue;
    17     root->left = NULL;
    18     root->right = NULL;
    19     
    20     if(startPreorder == endPreorder){                                                 //如果树的前序和中序只有一个节点,且相等则返回根节点,并致标志位为1
    21         if(startInorder == endInorder && *startPreorder == *startInorder){
    22             tags = 1;
    23             return root;
    24         }
    25         else{                                                          //否则将标志位置0
    26             //throw exception("Invalid input."); 
    27             tags = 0;
    28             return NULL;
    29         }
    30     }
    31     
    32     int *rootInorder = startInorder;
    33     
    34     while(rootInorder <= endInorder && *rootInorder != rootValue){                            //在中序序列中找到根节点的位置
    35         ++rootInorder;
    36     }
    37     
    38     if(rootInorder == endInorder && *rootInorder != rootValue){
    39     //    throw exception("Invalid input.");
    40         tags = 0;
    41         return NULL;
    42     }
    43     
    44     int leftLength = rootInorder - startInorder;
    45     int *leftPreorderEnd = startPreorder + leftLength;
    46     // 递归进行左右紫子树的构造
    47     if(leftLength > 0){
    48         root->left = construct(startPreorder + 1, leftPreorderEnd, startInorder, rootInorder - 1);
    49     }
    50     
    51     if(leftLength < endPreorder - startPreorder){
    52         root->right = construct(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder);
    53     }
    54     
    55     return root;
    56 }
    57 
    58 BinaryTreeNode* constructCore(int *preorder, int *inorder, int length){
    59     if(preorder == NULL || inorder == NULL || length <= 0){                   //检查是否构成树
    60         return NULL;
    61     }
    62     
    63     return construct(preorder, preorder + length - 1, inorder, inorder + length - 1);
    64 }
    65 
    66  void post_order(BinaryTreeNode *r)              //后序遍历输出节点
    67  {
    68      BinaryTreeNode *pNode = r;
    69      if(pNode != NULL)
    70      {
    71          post_order(pNode->left);
    72          post_order(pNode->right);
    73          cout<<pNode->data<<' ';
    74      }
    75  }
    76  
    77  int main(){
    78      int n;
    79      int pre[1010], in[1010];
    80      while(cin>>n){                            //输入格式定义,先输入节点数,再输入先序和中序序列
    81          for(int i = 0; i < n; i++){
    82              cin>>pre[i];
    83          }
    84          
    85          for(int j = 0; j < n; j++){
    86              cin>>in[j];
    87          }
    88      
    89         BinaryTreeNode *Root = constructCore(pre, in, n); 
    90     
    91         if(tags){
    92             post_order(Root);
    93         }
    94         else{
    95             cout<<"No"<<endl;
    96         }
    97     }
    98     return 0;
    99  }

  • 相关阅读:
    POJ 1681 Painter's Problem(高斯消元法)
    HDU 3530 Subsequence(单调队列)
    HDU 4302 Holedox Eating(优先队列或者线段树)
    POJ 2947 Widget Factory(高斯消元法,解模线性方程组)
    HDU 3635 Dragon Balls(并查集)
    HDU 4301 Divide Chocolate(找规律,DP)
    POJ 1753 Flip Game(高斯消元)
    POJ 3185 The Water Bowls(高斯消元)
    克琳:http://liyu.eu5.org
    WinDbg使用
  • 原文地址:https://www.cnblogs.com/dormant/p/5308636.html
Copyright © 2011-2022 走看看