重建二叉树

Posted 三颗心

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了重建二叉树相关的知识,希望对你有一定的参考价值。

题目

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

  例如,前序遍历序列:{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\\n", pNode->m_nValue);
 18 
 19         if(pNode->m_pLeft != NULL)
 20             printf("value of its left child is: %c.\\n", pNode->m_pLeft->m_nValue);
 21         else
 22             printf("left child is null.\\n");
 23 
 24         if(pNode->m_pRight != NULL)
 25             printf("value of its right child is: %c\\n", pNode->m_pRight->m_nValue);
 26         else
 27             printf("right child is null\\n");
 28     }
 29     printf("\\n");
 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("\\n\\n");
106     PrintTree(result);
107 }

  

以上是关于重建二叉树的主要内容,如果未能解决你的问题,请参考以下文章

4.重建二叉树

[编程题]重建二叉树

《剑指offer》— JavaScript重建二叉树

重建二叉树

重建二叉树-剑指Offer

用前序和中序重建二叉树 python