# 二叉树求第三种遍历序列

```// 树的结点的结构：
struct TreeNode{
TreeNode* LChild;
TreeNode* RChild;
char data;
};```

TreeNode* find_post_order(char* inOrder, char* preOrder, int length);

• 由于是前序，故而Ｇ必然是树根
• 递归对左子树进行求解，对右子树进行求解
• :以Ｇ切割后：G  DAFEMHZ　中序　ＡＤＥＦ    ＨＭＺ
```对于左子树，中序序列起始指针无需改变，靠length控制长度即可

```TreeNode* find_post_order(char* inOrder, char *preOrder, int length){
if(length == 0) return NULL;

int rootIndex;
for(rootIndex = 0; rootIndex < length; ++rootIndex){
if(inOrder[rootIndex] == *preOrder)
break;
}　　// 求前序序列的第一个结点（即ｒｏｏｔ结点）在中序序列中的坐标
TreeNode *rootNode = new TreeNode;
rootNode->data = *preOrder;

//LEFT  SubTree　注意子树中序, 前序的起始地点以及起到控制作用的序列长度
rootNode->LChild = find_post_order(inOrder, preOrder + 1, rootIndex);
//RIGHT SubTree
rootNode->RChild = find_post_order(inOrder + rootIndex + 1, preOrder + rootIndex + 1,length - (rootIndex + 1));
cout<<rootNode->data <<" ";           //最终输出 root结点
return rootNode;
}```

```TreeNode* find_pre_order(char* inOrder, char* postOrder, int length){
if(length == 0) return NULL;

int rootIndex;
for(rootIndex = 0; rootIndex < length; ++rootIndex){
if(inOrder[rootIndex] == *(postOrder + length - 1))
break;
}

TreeNode *rootNode = new TreeNode;
rootNode->data = *(postOrder + length - 1);
cout<<rootNode->data <<" ";
rootNode->LChild = find_pre_order(inOrder, postOrder, rootIndex);
rootNode->RChild = find_pre_order(inOrder + rootIndex + 1, postOrder + rootIndex, length - (rootIndex + 1));
return rootNode;
}```

(0)
(0)

0条