1. 程式人生 > >《劍指offer》第三十六題(二叉搜索樹與雙向鏈表)

《劍指offer》第三十六題(二叉搜索樹與雙向鏈表)

我們 troy 面試 next 指向 destroy 新的 left ext

// 面試題36:二叉搜索樹與雙向鏈表
// 題目:輸入一棵二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。要求
// 不能創建任何新的結點,只能調整樹中結點指針的指向。

#include <iostream>
#include "BinaryTree.h"
//這個程序看的我真的是頭大如牛

void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList);

BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree)
{
    BinaryTreeNode 
*pLastNodeInList = nullptr; ConvertNode(pRootOfTree, &pLastNodeInList);//看這個核心代碼 // pLastNodeInList指向雙向鏈表的尾結點,我們需要返回頭結點 BinaryTreeNode *pHeadOfList = pLastNodeInList; while (pHeadOfList != nullptr && pHeadOfList->m_pLeft != nullptr) pHeadOfList = pHeadOfList->m_pLeft;
return pHeadOfList; } void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList) { if (pNode == nullptr) return; BinaryTreeNode *pCurrent = pNode; if (pCurrent->m_pLeft != nullptr)//找到當前樹的最小節點 ConvertNode(pCurrent->m_pLeft, pLastNodeInList); pCurrent
->m_pLeft = *pLastNodeInList;//當前樹根的左孩子,指向左孩子子樹的最小值 if (*pLastNodeInList != nullptr) (*pLastNodeInList)->m_pRight = pCurrent;//把左孩子的m_pRight指向當前樹根 *pLastNodeInList = pCurrent; if (pCurrent->m_pRight != nullptr)//繼續把右面調整好 ConvertNode(pCurrent->m_pRight, pLastNodeInList); } // ====================測試代碼==================== void PrintDoubleLinkedList(BinaryTreeNode* pHeadOfList) { BinaryTreeNode* pNode = pHeadOfList; printf("The nodes from left to right are:\n"); while (pNode != nullptr) { printf("%d\t", pNode->m_nValue); if (pNode->m_pRight == nullptr) break; pNode = pNode->m_pRight; } printf("\nThe nodes from right to left are:\n"); while (pNode != nullptr) { printf("%d\t", pNode->m_nValue); if (pNode->m_pLeft == nullptr) break; pNode = pNode->m_pLeft; } printf("\n"); } void DestroyList(BinaryTreeNode* pHeadOfList) { BinaryTreeNode* pNode = pHeadOfList; while (pNode != nullptr) { BinaryTreeNode* pNext = pNode->m_pRight; delete pNode; pNode = pNext; } } void Test(const char* testName, BinaryTreeNode* pRootOfTree) { if (testName != nullptr) printf("%s begins:\n", testName); PrintTree(pRootOfTree); BinaryTreeNode* pHeadOfList = Convert(pRootOfTree); PrintDoubleLinkedList(pHeadOfList); } // 10 // / // 6 14 // /\ /// 4 8 12 16 void Test1() { BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); BinaryTreeNode* 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() { BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* 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() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* 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() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); Test("Test4", pNode1); DestroyList(pNode1); } // 樹中沒有結點 void Test5() { Test("Test5", nullptr); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); system("pause"); return 0; }

《劍指offer》第三十六題(二叉搜索樹與雙向鏈表)