重建二叉树(根据 前序 和 中序二叉树)

来源:互联网 发布:无线路由器网络模式 编辑:程序博客网 时间:2024/05/22 18:14



下面是源代码:


#include <stdio.h>    struct BinaryTreeNode  {      int m_nValue;      BinaryTreeNode *m_pLeft;      BinaryTreeNode *m_pRight;  };    BinaryTreeNode *ConstructCore(int *startPreorder, int *endPreorder, int *startInorder, int *endInorder)  {      int rootValue = startPreorder[0];      BinaryTreeNode *root = new BinaryTreeNode();      root->m_nValue = rootValue;      root->m_pLeft = NULL;      root->m_pRight = NULL;        //递归结束条件,当只剩节点时      if(startPreorder == endPreorder)      {          if(startInorder == endInorder && *startPreorder == *startInorder)          {              return root;          }else          {              return NULL;          }      }        int *rootInorder = startInorder;      while(rootInorder <= endInorder && *rootInorder != rootValue)      {          rootInorder++;      }        if(rootInorder > endInorder)      {          printf("无效的输入\n");      }        int leftLength = rootInorder - startInorder;      int rightLength = endInorder - rootInorder;        if(leftLength > 0)      {          root->m_pLeft = ConstructCore(startPreorder + 1, startPreorder + leftLength, startInorder, startInorder + leftLength - 1);      }        if(rightLength > 0)      {          root->m_pRight = ConstructCore(startPreorder + leftLength + 1, endPreorder, rootInorder + 1, endInorder);      }        return root;  }    BinaryTreeNode *Construct(int *preorder, int *inorder, int length)  {      if(preorder == NULL, inorder == NULL, length <= 0)      {          return NULL;      }        return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);  }    void PrintTreeNode(BinaryTreeNode* pNode)  {      if(pNode != NULL)      {          printf("value of this node is: %d\n", pNode->m_nValue);            if(pNode->m_pLeft != NULL)              printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);          else              printf("left child is null.\n");            if(pNode->m_pRight != NULL)              printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);          else              printf("right child is null.\n");      }      else      {          printf("this node is null.\n");      }        printf("\n");  }    void PrintTree(BinaryTreeNode* pRoot)  {      PrintTreeNode(pRoot);        if(pRoot != NULL)      {          if(pRoot->m_pLeft != NULL)              PrintTree(pRoot->m_pLeft);            if(pRoot->m_pRight != NULL)              PrintTree(pRoot->m_pRight);      }  }    void DestroyTree(BinaryTreeNode* pRoot)  {      if(pRoot != NULL)      {          BinaryTreeNode* pLeft = pRoot->m_pLeft;          BinaryTreeNode* pRight = pRoot->m_pRight;            delete pRoot;          pRoot = NULL;            DestroyTree(pLeft);          DestroyTree(pRight);      }  }    void main()  {      const int length = 8;      int preorder[length] = {1, 2, 4, 7, 3, 5, 6, 8};      int inorder[length] = {4, 7, 2, 1, 5, 3, 8, 6};        BinaryTreeNode *root = Construct(preorder, inorder, length);      PrintTree(root);      DestroyTree(root);  }  

运行结果:


注:本题用递归解,首先得分析好每次递归做的相同事情是什么,其次,判断终止条件;最后,注意边界条件和异常处理。

递归虽然简洁,但它同时也有显著的缺点。

         1、递归由于是函数调用自身,而函数调用是有时间和空间消耗的:每一次函数调用,都需要在内存栈中分配空间以保存参数、返回地址及临时变量,而且往栈里面压入数据和弹出数据都需要时间。这就不难理解递归效率不如非递归。

         2、递归可能会引起严重的问题:调用栈溢出。前面分析中提到徐哎哟为每一次函数调用在内存栈中分配空间,而每个进程的栈的容量是有限的。递归调用的层级太多时,就会超出栈的容量,从而导致调用栈溢出。

 

参考:

剑指offer

http://blog.csdn.net/zhaojinjia/article/details/9305251

http://blog.csdn.net/ljf913/article/details/8576771


0 0
原创粉丝点击