1 /****************************************                                                                                                   2     > File Name:test.c  3     > Author:xiaoxiaohui  4     > mail:1924224891@qq.com  5     > Created Time:2016年05月20日 星期五 16时11分40秒  6 ****************************************/  7   8 #include
  9  10 BinaryTreeNode* CreateTree(int* first, int* second, int len) 11 { 12     if(first == NULL || second == NULL || len <= 0 ) 13     { 14         return NULL; 15     } 16  17     int num = first[0]; 18     BinaryTreeNode* node = new BinaryTreeNode(num);      //写入根节点的值 19  20     int i  = 0; 21     for(;i < len;i++)    //在中序数列中找到与根节点值相同的值 22     { 23         if(second[i] == num) 24         { 25             break; 26         } 27     } 28  29     node->m_pLeft = CreateTree(first[1], second[0], i);    //递归创建左子树 30     node->m_pRight = CreateTree(second[i + 1], second[i + 1], len - i - 1);   //递归创建右子树 31  32     return first[0];     //返回根节点 33 } 34  35  36  37 //总结:当遇到一般的情况与第一种情况相同时,可以考虑递归算法。 38 //当用递归算法时,要先找出一般规律,然后根据这个一般规律进行写代码