问: 一颗二叉树前序遍历为{1,2,4,7,3,5,6,8},中序遍历为 {4,7,2,1,5,3,8,6},构建这只二叉树。
思路:根节点为1,根据中序遍历左边为4,7,2,右边为5,3,8,6。可以写一个递归函数来搞。
传入参数分别为:前序开始,前序结束,后序开始,后序结束
struct BinaryTreeNode
{
int m_value;
BinaryTreeNode* m_pLeftNode;
BinaryTreeNode* m_pRightNode;
};
BinaryTreeNode* ConstructCore(int* startPrerder, int* endPreorder,int* startInorder,int* endInorder)
{
//构建根节点
int rootValue = startPrerder[0];
BinaryTreeNode *rootNode = new BinaryTreeNode();
rootNode->m_value = rootVal