1 int main() { 2 char c[] = "ABCDEFGHIJ"; 3 pTreeNode_t pArr[MAX_SIZE_OF_TREE]; 4 for (int i = 0; i < MAX_SIZE_OF_TREE; i++) { 5 pArr[i] = (pTreeNode_t)calloc(1, sizeof(TreeNode_t)); 6 pArr[i]->val = c[i]; 7 } 8 //将左子数和右子数树连到根,index_link_to指现在需要连接的父节点,index_going指正在遍历的节点作子节点 9 //index_link_to 0A 1B …… 10 //左索引 1B 3D 11 //右索引 2C 4E 12 for (int index_link_to = 0, index_going = 1; index_going < MAX_SIZE_OF_TREE; index_going++) { 13 if (pArr[index_link_to]->pLeft == NULL) { 14 pArr[index_link_to]->pLeft = pArr[index_going]; 15 } 16 else { 17 pArr[index_link_to]->pRight = pArr[index_going]; 18 index_link_to++; 19 } 20 } 21 22 return 0; 23 }
原文:https://www.cnblogs.com/Ping697/p/14370444.html