剑指Offer----面试题27:二叉搜索树与双向链表
来源:互联网 发布:t lab软件下载 编辑:程序博客网 时间:2024/06/10 07:30
题目:
输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。如下图中的二叉搜索树,则输出转换之后的排序双向链表。
分析:
中序遍历算法的特点是按照从小到大的顺序遍历二叉树的每一个结点。当遍历到根节点的时候,可以把树分为三部分:值为10的结点,根节点值为6的左子树,根节点值为14的右子树。根据排序链表的定义,将根节点与左子树最大的一个结点连接起来,同时将该根节点与右子树最小的一个结点连接起来。如下图所示,再用同样的方法处理左子树和右子树(递归)。
源代码如下:
BinaryTreeNode *Covert(BinaryTreeNode *pHead){if (pHead == NULL)return NULL;BinaryTreeNode *pLastNode = NULL;ConvertNode(pHead, &pLastNode);//pLastNode指向双向链表的尾结点//我们需要返回头结点BinaryTreeNode *pHeadNode = pLastNode;while (pHeadNode != NULL && pHeadNode->left != NULL)pHeadNode = pHeadNode->left;return pHeadNode;}void ConvertNode(BinaryTreeNode *node, BinaryTreeNode **pLastNode){if (node == NULL)return;BinaryTreeNode *current = node;if (current->left != NULL)ConvertNode(current->left, pLastNode);current->left = *pLastNode;if (*pLastNode != NULL)(*pLastNode)->right = current;*pLastNode = current;if (current->right != NULL)ConvertNode(current->right, pLastNode);}
官方源代码:
#include"BinaryTree.h"#include<cstdlib>#include<cstdio>using namespace OrdinaryBinaryTreeSpace6;void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList);BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree){BinaryTreeNode *pLastNodeInList = NULL;ConvertNode(pRootOfTree, &pLastNodeInList);// pLastNodeInList指向双向链表的尾结点,// 我们需要返回头结点BinaryTreeNode *pHeadOfList = pLastNodeInList;while (pHeadOfList != NULL && pHeadOfList->left != NULL)pHeadOfList = pHeadOfList->left;return pHeadOfList;}void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList){if (pNode == NULL)return;BinaryTreeNode *pCurrent = pNode;if (pCurrent->left != NULL)ConvertNode(pCurrent->left, pLastNodeInList);pCurrent->left = *pLastNodeInList;if (*pLastNodeInList != NULL)(*pLastNodeInList)->right = pCurrent;*pLastNodeInList = pCurrent;if (pCurrent->right != NULL)ConvertNode(pCurrent->right, pLastNodeInList);}// ====================测试代码====================void PrintDoubleLinkedList(BinaryTreeNode* pHeadOfList){BinaryTreeNode* pNode = pHeadOfList;printf("The nodes from left to right are:\n");while (pNode != NULL){printf("%d\t", pNode->element);if (pNode->right == NULL)break;pNode = pNode->right;}printf("\nThe nodes from right to left are:\n");while (pNode != NULL){printf("%d\t", pNode->element);if (pNode->left == NULL)break;pNode = pNode->left;}printf("\n");}void DestroyList(BinaryTreeNode* pHeadOfList){BinaryTreeNode* pNode = pHeadOfList;while (pNode != NULL){BinaryTreeNode* pNext = pNode->right;delete pNode;pNode = pNext;}}void Test(char* testName, BinaryTreeNode* pRootOfTree){if (testName != NULL)printf("%s begins:\n", testName);PrintTreeMid(pRootOfTree);BinaryTreeNode* pHeadOfList = Convert(pRootOfTree);PrintDoubleLinkedList(pHeadOfList);}// 10// / \// 6 14// /\ /\// 4 8 12 16void 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);ConnectBinaryTreeNodes(pNode10, pNode6, pNode14);ConnectBinaryTreeNodes(pNode6, pNode4, pNode8);ConnectBinaryTreeNodes(pNode14, pNode12, pNode16);Test("Test1", pNode10);DestroyList(pNode4);}// 5// /// 4// /// 3// /// 2// /// 1void Test2(){BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);ConnectBinaryTreeNodes(pNode5, pNode4, NULL);ConnectBinaryTreeNodes(pNode4, pNode3, NULL);ConnectBinaryTreeNodes(pNode3, pNode2, NULL);ConnectBinaryTreeNodes(pNode2, pNode1, NULL);Test("Test2", pNode5);DestroyList(pNode1);}// 1// \// 2// \// 3// \// 4// \// 5void Test3(){BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);ConnectBinaryTreeNodes(pNode1, NULL, pNode2);ConnectBinaryTreeNodes(pNode2, NULL, pNode3);ConnectBinaryTreeNodes(pNode3, NULL, pNode4);ConnectBinaryTreeNodes(pNode4, NULL, pNode5);Test("Test3", pNode1);DestroyList(pNode1);}// 树中只有1个结点void Test4(){BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);Test("Test4", pNode1);DestroyList(pNode1);}// 树中没有结点void Test5(){Test("Test5", NULL);}int main(){Test1();Test2();Test3();Test4();Test5();system("pause");return 0;}
运行结果:
Test1 begins:4 6 8 10 12 14 16 The nodes from left to right are:4 6 8 10 12 14 16The nodes from right to left are:16 14 12 10 8 6 4Test2 begins:1 2 3 4 5 The nodes from left to right are:1 2 3 4 5The nodes from right to left are:5 4 3 2 1Test3 begins:1 2 3 4 5 The nodes from left to right are:1 2 3 4 5The nodes from right to left are:5 4 3 2 1Test4 begins:1 The nodes from left to right are:1The nodes from right to left are:1Test5 begins:The tree is emptyThe nodes from left to right are:The nodes from right to left are:请按任意键继续. . .
0 0
- [剑指offer][面试题27]二叉搜索树与双向链表
- [剑指offer]面试题27 二叉搜索树与双向链表
- 剑指Offer:面试题27 二叉搜索树与双向链表
- 《剑指Offer》 面试题27:二叉搜索树与双向链表
- 《剑指Offer》学习笔记--面试题27:二叉搜索树与双向链表
- 剑指offer 面试题27—二叉搜索树与双向链表
- 剑指offer--面试题27:二叉搜索树与双向链表--Java实现
- 【剑指Offer学习】【面试题27:二叉搜索树与双向链表】
- 剑指Offer面试题27(Java版):二叉搜索树与双向链表
- 剑指offer之面试题27:二叉搜索树与双向链表
- 剑指offer之面试题27二叉搜索树与双向链表
- 剑指Offer----面试题27:二叉搜索树与双向链表
- 剑指Offer-面试题27:二叉搜索树与双向链表
- 剑指Offer:面试题27——二叉搜索树与双向链表(java实现)
- 剑指offer面试题27:二叉搜索树与双向链表
- 剑指offer--面试题27:二叉搜索树与双向链表
- 剑指Offer面试题27(Java版):二叉搜索树与双向链表
- 剑指offer——面试题27:二叉搜索树与双向链表
- 字符编码的故事
- AVPlayer
- ios进阶--Autolayout 之VFL语言
- 微信开发之ngrok内网穿透工具
- Java开发常见问题
- 剑指Offer----面试题27:二叉搜索树与双向链表
- 解决Xshell显示中文乱码的问题
- WordPress + Avada主题 安装Demo教程
- C数组和指针相关的问题
- 8255 1 方式并行接口设计
- 加密网址方法csdn.net
- 1号店11.11:从应用架构落地点谈高可用高并发高性能
- ubuntu下讲eclipse直接部署至tomcat上
- Bootstrap 栅格系统