• 重建二叉树


    题目

      输入某二叉树的前序遍历和中序遍历,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含有重复的数字。

      例如,前序遍历序列:{1,2,3,7,3,5,6,8},中序遍历序列:{4,7,2,1,5,3,8,6}

    答案

      前序遍历:

        前序遍历首先访问根结点然后遍历左子树,最后遍历右子树。在遍历左、右子树时,仍然先访问根结点,然后遍历左子树,最后遍历右子树。

      中序遍历:

        中序遍历首先遍历左子树,然后访问根结点,最后遍历右子树。在遍历左、右子树时,仍然先遍历左子树,再访问根结点,最后遍历右子树。

     

      1 #include "stdafx.h"
      2 #include<deque>
      3 #include<iostream>
      4 #define TREElEN 6
      5 
      6 struct BinaryTreeNode
      7 {
      8     int                   m_nValue;
      9     BinaryTreeNode*       m_pLeft;
     10     BinaryTreeNode*       m_pRight;
     11 };
     12 
     13 void PrintTreeNode(BinaryTreeNode* pNode)
     14 {
     15     if(pNode != NULL)
     16     {
     17         printf("value of this node is: %c
    ", pNode->m_nValue);
     18 
     19         if(pNode->m_pLeft != NULL)
     20             printf("value of its left child is: %c.
    ", pNode->m_pLeft->m_nValue);
     21         else
     22             printf("left child is null.
    ");
     23 
     24         if(pNode->m_pRight != NULL)
     25             printf("value of its right child is: %c
    ", pNode->m_pRight->m_nValue);
     26         else
     27             printf("right child is null
    ");
     28     }
     29     printf("
    ");
     30 }
     31 
     32 void PrintTree(BinaryTreeNode* pRoot)
     33 {
     34     PrintTreeNode(pRoot);
     35 
     36     if(pRoot != NULL)
     37     {
     38         if(pRoot->m_pLeft != NULL)
     39             PrintTree(pRoot->m_pLeft);
     40 
     41         if(pRoot->m_pRight != NULL)
     42             PrintTree(pRoot->m_pRight);
     43     }
     44 }
     45 
     46 BinaryTreeNode* rebuild(char *preOrder, char* inOrder, int length)
     47 {
     48     if(preOrder == NULL || inOrder == NULL || length <= 0)
     49         return NULL;
     50 
     51     char c = preOrder[0];
     52 
     53     BinaryTreeNode* root = new BinaryTreeNode();
     54     root->m_nValue = c;
     55     root->m_pRight = root->m_pLeft = NULL;
     56 
     57     int i ;
     58     for(i = 0 ; i < length && inOrder[i] != c ; i++);
     59     int leftLength = i;
     60     int rightLength = length - i - 1;
     61 
     62     if(leftLength > 0)
     63             root->m_pLeft = rebuild(&preOrder[1],&inOrder[0],leftLength);
     64 
     65     if(rightLength>0)
     66             root->m_pRight = rebuild(&preOrder[leftLength + 1], &inOrder[leftLength+1], rightLength);
     67     return root;
     68 }
     69 
     70 void PrintFromTopToBottom(BinaryTreeNode* pRoot)
     71 {
     72     if(pRoot == NULL)
     73         return;
     74 
     75     std::deque<BinaryTreeNode *> dequeTreeNode;
     76 
     77     dequeTreeNode.push_back(pRoot);
     78 
     79     while(dequeTreeNode.size())
     80     {
     81         BinaryTreeNode *pNode = dequeTreeNode.front();
     82         dequeTreeNode.pop_front();
     83 
     84         printf("%c ", pNode->m_nValue);
     85 
     86         if(pNode->m_pLeft)
     87             dequeTreeNode.push_back(pNode->m_pLeft);
     88 
     89         if(pNode->m_pRight)
     90             dequeTreeNode.push_back(pNode->m_pRight);
     91     }
     92 }
     93 // 普通二叉树
     94 //              a
     95 //           /     
     96 //          b       c  
     97 //         /       / 
     98 //        d       e   f
     99 int main()
    100 {
    101     char PreOrder[TREElEN] = {'a', 'b', 'd', 'c', 'e', 'f'};
    102     char InOrder[TREElEN] =  {'d', 'b', 'a', 'e', 'c', 'f'};
    103     BinaryTreeNode* result = rebuild(PreOrder, InOrder, 6);
    104     PrintFromTopToBottom(result);
    105     printf("
    
    ");
    106     PrintTree(result);
    107 }

      

  • 相关阅读:
    Day 31 Event事件/进程池和线程池/高性能爬取梨视频/协程
    scp命令
    [Influxdb]记录
    REMOTE HOST IDENTIFICATION HAS CHANGED!
    【转载】Java异常控制机制和异常处理原则
    html转成PDF,替换内容
    SOAP请求-示例
    InputStream转成String
    [集合操作]List对象数组获取元素值非空对象及根据对象元素值排序取最大&取对象数组的对象元素集合&条件去重&条件分组
    maven deploy jar包到远程仓库400
  • 原文地址:https://www.cnblogs.com/sankexin/p/5612255.html
Copyright © 2020-2023  润新知