分析:
1. 二叉树的中序遍历正好是排好序的遍历方式,因此可以采用中序递归的方式来处理;
2. 可以用类似输出流的方式来”输出“节点到链表末尾;
3. 可以用局部变量来简化判断,优化程序。
程序:
typedef struct tagTreeNode_s
{
int nValue;
tagTreeNode_s* pLeftNode;
tagTreeNode_s* pRightNode;
} TreeNode_s;
void AppendNode(TreeNode_s* pNode, TreeNode_s*& pTailNode)
{
if (pNode == NULL)
return;
pTailNode->pRightNode = pNode;
pNode->pLeftNode = pTailNode;
pTailNode = pNode;
}
void AppendTree(TreeNode_s* pRootNode, TreeNode_s*& pTailNode)
{
if (pRootNode == NULL)
return;
AppendTree(pRootNode->pLeftNode, pTailNode);
AppendNode(pRootNode, pTailNode);
AppendTree(pRootNode->pRightNode, pTailNode);
}
TreeNode_s* ToLink(TreeNode_s* pRootNode)
{
TreeNode_s HeadNode;
TreeNode_s* pTailNode = &HeadNode;
pTailNode->pRightNode = NULL;
AppendTree(pRootNode, pTailNode);
pTailNode->pRightNode = NULL;
pTailNode = HeadNode->pRightNode;
if (pTailNode != NULL)
pTailNode->pLeftNode = NULL;
return pTailNode;
}