void convertNode(BSTreeNode *root, BSTreeNode ** pLastNodeInList) { if(!root) return ; if(root->left) { convertNode(root->left, pLastNodeInList); } root->left = *pLastNodeInList; if(*pLastNodeInList != NULL) (*pLastNodeInList)->right = root; *pLastNodeInList = root; if(root->right) { convertNode(root->right, pLastNodeInList); } } BSTreeNode *convert(BSTreeNode* root)//true = right, false = left { if(!root) return root; BSTreeNode* pLastNodeInList = NULL; convertNode(root, &pLastNodeInList); while(pLastNodeInList && pLastNodeInList->left) { pLastNodeInList = pLastNodeInList->left; } return pLastNodeInList; }
输入一个二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。不能创建任何新的结点,只能调整树中结点指针的指向。
时间: 2024-12-13 07:28:02