【剑指】36.二叉搜索树与双向链表

题目描述

  • 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

算法分析

  • 中序遍历,并记录一个pre指针即可。

提交代码:

class Solution {
public:
	TreeNode* Convert(TreeNode* pRootOfTree)
	{
		if (!pRootOfTree)
			return nullptr;

		TreeNode* pPreNode = nullptr;
		ConvertCore(pRootOfTree, &pPreNode);

		TreeNode* pNewHead = pRootOfTree;
		while (pNewHead->left)
			pNewHead = pNewHead->left;

		return pNewHead;
	}

	void ConvertCore(TreeNode* pRootOfTree, TreeNode** pPreNode)
	{
		if (pRootOfTree->left)
			ConvertCore(pRootOfTree->left, pPreNode);

		pRootOfTree->left = *pPreNode;

		if (*pPreNode)
			(*pPreNode)->right = pRootOfTree;

		*pPreNode = pRootOfTree;

		if (pRootOfTree->right)
			ConvertCore(pRootOfTree->right, pPreNode);
	}

};

测试代码:

// ====================测试代码====================
void PrintDoubleLinkedList(TreeNode* pHeadOfList)
{
	TreeNode* pNode = pHeadOfList;

	printf("The nodes from left to right are:\n");
	while (pNode != nullptr)
	{
		printf("%d\t", pNode->val);

		if (pNode->right == nullptr)
			break;
		pNode = pNode->right;
	}

	printf("\nThe nodes from right to left are:\n");
	while (pNode != nullptr)
	{
		printf("%d\t", pNode->val);

		if (pNode->left == nullptr)
			break;
		pNode = pNode->left;
	}

	printf("\n");
}

void DestroyList(TreeNode* pHeadOfList)
{
	TreeNode* pNode = pHeadOfList;
	while (pNode != nullptr)
	{
		TreeNode* pNext = pNode->right;

		delete pNode;
		pNode = pNext;
	}
}

void Test(char* testName, TreeNode* pRootOfTree)
{
	if (testName != nullptr)
		printf("%s begins:\n", testName);

	PrintTree(pRootOfTree);

	Solution s;
	TreeNode* pHeadOfList = s.Convert(pRootOfTree);

	PrintDoubleLinkedList(pHeadOfList);
}

//            10
//         /      \
//        6        14
//       /\        /\
//      4  8     12  16
void Test1()
{
	TreeNode* pNode10 = CreateBinaryTreeNode(10);
	TreeNode* pNode6 = CreateBinaryTreeNode(6);
	TreeNode* pNode14 = CreateBinaryTreeNode(14);
	TreeNode* pNode4 = CreateBinaryTreeNode(4);
	TreeNode* pNode8 = CreateBinaryTreeNode(8);
	TreeNode* pNode12 = CreateBinaryTreeNode(12);
	TreeNode* pNode16 = CreateBinaryTreeNode(16);

	ConnectTreeNodes(pNode10, pNode6, pNode14);
	ConnectTreeNodes(pNode6, pNode4, pNode8);
	ConnectTreeNodes(pNode14, pNode12, pNode16);

	Test("Test1", pNode10);

	DestroyList(pNode4);
}

//               5
//              /
//             4
//            /
//           3
//          /
//         2
//        /
//       1
void Test2()
{
	TreeNode* pNode5 = CreateBinaryTreeNode(5);
	TreeNode* pNode4 = CreateBinaryTreeNode(4);
	TreeNode* pNode3 = CreateBinaryTreeNode(3);
	TreeNode* pNode2 = CreateBinaryTreeNode(2);
	TreeNode* pNode1 = CreateBinaryTreeNode(1);

	ConnectTreeNodes(pNode5, pNode4, nullptr);
	ConnectTreeNodes(pNode4, pNode3, nullptr);
	ConnectTreeNodes(pNode3, pNode2, nullptr);
	ConnectTreeNodes(pNode2, pNode1, nullptr);

	Test("Test2", pNode5);

	DestroyList(pNode1);
}

// 1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
void Test3()
{
	TreeNode* pNode1 = CreateBinaryTreeNode(1);
	TreeNode* pNode2 = CreateBinaryTreeNode(2);
	TreeNode* pNode3 = CreateBinaryTreeNode(3);
	TreeNode* pNode4 = CreateBinaryTreeNode(4);
	TreeNode* pNode5 = CreateBinaryTreeNode(5);

	ConnectTreeNodes(pNode1, nullptr, pNode2);
	ConnectTreeNodes(pNode2, nullptr, pNode3);
	ConnectTreeNodes(pNode3, nullptr, pNode4);
	ConnectTreeNodes(pNode4, nullptr, pNode5);

	Test("Test3", pNode1);

	DestroyList(pNode1);
}

// 树中只有1个结点
void Test4()
{
	TreeNode* pNode1 = CreateBinaryTreeNode(1);
	Test("Test4", pNode1);

	DestroyList(pNode1);
}

// 树中没有结点
void Test5()
{
	Test("Test5", nullptr);
}

int main(int argc, char* argv[])
{
	Test1();
	Test2();
	Test3();
	Test4();
	Test5();

	return 0;
}

你可能感兴趣的:(剑指offer)