剑指Offer04 重建二叉树

代码有问题

  1 /*************************************************************************
  2     > File Name: 04_Rebuild_BinaryTree.c
  3     > Author: Juntaran
  4     > Mail: JuntaranMail@gmail.com
  5     > Created Time: 2016年08月29日 星期一 16时32分35秒
  6  ************************************************************************/
  7 
  8 #include <stdio.h>
  9 #include <malloc.h>
 10 
 11 // 二叉树结构体
 12 struct BinaryTree
 13 {
 14     int val;
 15     struct BinaryTree* left;
 16     struct BinaryTree* right;
 17 };
 18 
 19 void preorderPrintBinaryTree(struct BinaryTree* root)
 20 {
 21     if (root == NULL)
 22         return;
 23     printf("%d ", root->val);
 24     preorderPrintBinaryTree(root->left);
 25     preorderPrintBinaryTree(root->right);
 26 }
 27 
 28 void inorderPrintBinaryTree(struct BinaryTree* root)
 29 {
 30     if (root == NULL)
 31         return;
 32     preorderPrintBinaryTree(root->left);
 33     printf("%d ", root->val);
 34     preorderPrintBinaryTree(root->right);
 35 }
 36 
 37 void postorderPrintBinaryTree(struct BinaryTree* root)
 38 {
 39     if (root == NULL)
 40         return;
 41     preorderPrintBinaryTree(root->left);
 42     preorderPrintBinaryTree(root->right);
 43     printf("%d ", root->val);
 44 }
 45 
 46 
 47 
 48 struct BinaryTree* rebuild_BinaryTree(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder)
 49 {
 50     // 前序遍历第一个数字是根节点的值
 51     int rootValue = startPreorder[0];
 52     struct BinaryTree* root = (BinaryTree*)malloc(sizeof(BinaryTree));
 53     root->val = rootValue;
 54     root->left = NULL;
 55     root->right = NULL;
 56     
 57     if (startPreorder == endPreorder)
 58     {
 59         if (startInorder==endInorder && *startPreorder==*startInorder)
 60             return root;
 61     }
 62     
 63     // 在前序遍历中找根节点的值
 64     int* rootInorder = startInorder;
 65     while (rootInorder<=endInorder && *rootInorder!=rootValue)
 66         ++ rootInorder;
 67     
 68     int leftLength = rootInorder - startInorder;
 69     int* leftPreorderEnd = startPreorder + leftLength;
 70     if (leftLength > 0)
 71     {
 72         // 构建左子树
 73         root->left = rebuild_BinaryTree(startPreorder+1, leftPreorderEnd, startInorder, rootInorder-1);
 74     }
 75     if (leftLength < endPreorder - startPreorder)
 76     {
 77         // 构建右子树
 78         root->right = rebuild_BinaryTree(leftPreorderEnd+1, endPreorder, rootInorder+1, endInorder);
 79     }
 80     return root;
 81 }
 82 
 83 struct BinaryTree* rebuild(int* preorder, int* inorder, int length)
 84 {
 85     if (preorder==NULL || inorder==NULL || length<=0)
 86         return NULL;
 87     
 88     return rebuild_BinaryTree(preorder, preorder+length-1, inorder, inorder+length-1);
 89 }
 90 
 91 // ====================测试代码====================
 92 void Test(int* preorder, int* inorder, int length)
 93 {
 94     printf("The preorder sequence is: ");
 95     for(int i = 0; i < length; ++ i)
 96         printf("%d ", preorder[i]);
 97     printf("\n");
 98 
 99     printf("The inorder  sequence is: ");
100     for(int i = 0; i < length; ++ i)
101         printf("%d ", inorder[i]);
102     printf("\n");
103 
104     struct BinaryTree* root = rebuild(preorder, inorder, length);
105     
106     printf("After rebuild:\n");
107     printf("\nThe preorder  sequence is: ");
108     preorderPrintBinaryTree(root);
109     printf("\nThe inorder   sequence is: ");
110     inorderPrintBinaryTree(root);
111     printf("\nThe postorder sequence is: ");
112     postorderPrintBinaryTree(root);
113 }
114 
115 int main()
116 {
117     int length = 8;
118     int preorder[8] = {1, 2, 4, 7, 3, 5, 6, 8};
119     int inorder[8]  = {4, 7, 2, 1, 5, 3, 8, 6};
120     
121     Test(preorder, inorder, length);
122 }

 

posted @ 2016-08-29 19:40  Juntaran  阅读(156)  评论(0编辑  收藏  举报