【Data structure & Algorithm】把二元查找树转变成排序的双向链表
把二元查找树转变成排序的双向链表
题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表,要求不能创建任何新节点,只调整指针指向。
比如将二元查找树
10
/ \
6 14
/ \ / \
4 8 12 16
转换成双向链表4=6=8=10=12=14=16
分析:
思路一:当到达某一节点准备调整以该节点为根节点的子树时,先调整其左子树将左子树转换成一个排好序的左子链表,再调整其右子树转换右子链表。最近链接左子链表的最右节点(左子树的最大节点)、当前节点和有子链表的最左节点(右子树的最小节点)。从树的根节点开始递归调整所有节点。
首先定义二元查找树的数据结构如下:
structBSTreeNode
{
int m_nValue;
BSTreeNode *m_pLeft;
BSTreeNode *m_pRight;
}
BSTreeNode*ConvertNode(BSTreeNode* pNode, bool asRight)
{
if (!pNode)
returnNULL;
BSTreeNode *pLeft = NULL;
BSTreeNode* pRight = NULL; //Convert the left sub-tree
if(pNode -> m_pLeft)
pLeft = ConvertNode(pNode ->m_pLeft, false); //Convert the greatest node in the leftsub-tree to the current node
if (pLeft)
{
pLeft -> m_pRight = pNode;
pNode -> m_pLeft = pLeft;
} //Convert the right sub-tree
if (pNode -> m_pRight)
pRight= ConvertNode (pNode -> m_pRight, true); //Connect the least node in the rightsub-tree to the current node
if (pRight)
{
pNode-> m_pRight = pRight;
pRight-> m_pLeft = pNode;
} BSTreeNode *pTemp = pNode; //If the current node is the right child ofits parent, return the least node in the tree whose root is the current node
if (asRight)
{
while(pTemp -> m_pLeft)
pTemp= pTemp -> m_pLeft;
}
//if the current node is the left child ofits parent, return the greatest node in the tree whose root is the current node
else
{
while(pTemp -> m_pRight)
pTemp= pTemp -> m_pRight;
}
return pTemp; //Convert a BSTree into a sorteddouble-linked list
BSTreeNode* Convert(BSTreeNode* pHeadOfTree)
{
returnConvertNode(pHeadOfTree, true);
}
思路二:中序遍历二叉树。较小节点先访问。如果访问一个节点,假设之前访问过的节点已经调整成一个排序的双向链表,再把调整当前节点的指针将其连接到链表末尾,当所有节点都访问过之后,整棵树也就转换为一个排序的双向链表了。
voidConvertNode(BSTreeNode* pNode, BSTreeNode*& pLastNodeInList)
{
if(pNode ==NULL)
return;
BSTreeNode *pCurrent = pNode; //Convert the left sub-tree
if (pCurrent -> m_pLeft !=NULL)
ConvertNode(pCurrent-> m_pLeft, pLastNodeInList); //Put the current node into thedouble-linked list
pCurrent -> m_pLeft = pLastNodeInList;
if(pLastNodeInList != NULL)
pLastNodeInList-> m_pRight = pCurrent; pLastNodeInList = pCurrent; //Convert the right sub-tree
if (pCurrent -> m_pRight != NULL)
ConvertNode(pCurrent-> m_pRight, pLastNodeInList); //Convert a BSTree into a sorteddouble-linked list
BSTreeNode* Convert_Solution(BSTreeNode*pHeadOfTree)
{
BSTreeNode*pLastNodeInList = NULL;
ConvertNode(pHeadOfTree,pLastNodeInList); //Getthe head of the double-linked list
BSTreeNode*pHeadOfList = pLastNodeInList;
while(pHeadOfList && pHeadOfList -> m_pLeft)
pHeadOfList= pHeadOfList -> m_pLeft; return pHeadOfList;
}
}
【Data structure & Algorithm】把二元查找树转变成排序的双向链表的更多相关文章
- MS - 1 - 把二元查找树转变成排序的双向链表
## 1. 把二元查找树转变成排序的双向链表 ## ### 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表. ### 要求不能创建任何新的结点,只调整指针的指向. 10 ...
- 二元查找树转变成排序的双向链表之C#算法实现
此题为July在CSDN发布的微软编程面试100题中的第一题,觉得蛮有趣的,今天也拿过来玩玩,July的代码用的是C++实现,可能因为有指针的原因吧,感觉看起来相对比较容易理解整个的实现过程,而我,试 ...
- 1.把二元查找树转变成排序的双向链表[BST2DoubleLinkedList]
[题目]:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调整指针的指向. 比如将二元查找树 . 10 / \ 6 14 / \ / \ 4 8 12 16 转 ...
- IT公司100题-15-求二元查找树的镜像
问题描述: 输入一颗二元查找树,将该树转换为它的镜像树,即对每一个节点,互换左右子树. 例如输入: 6/ \4 12/ \ / \2 5 8 16 输出: 6/ ...
- IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果
问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...
- 6.二元查找树的后序遍历结果[PostOrderOfBST]
[题目] 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: 8 ...
- 11.求二元查找树的镜像[MirrorOfBST]
[题目] 输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点.用递归和循环两种方法完成树的镜像转换. 例如输入: 8 / \ 6 1 ...
- [Data Structure & Algorithm] 七大查找算法
查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找.本文简单概括性的介绍了常见的七种查找算法,说是七种,其实二分查找.插值查找以及斐波那契查找 ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- AMD移动FP5平台时序解释
好文章推荐:https://wenku.baidu.com/view/199379576137ee06eef91828.html AMD(FP5封装)时序全解. 由于刚开始接触AMD移动平台,难免有错 ...
- Mataplotlib事例操作
刚开始需要的文件是和前边的两个连载一起的
- Python--数据类型整理
数据类型整理-------------------------------------------------------------------------------------------- ...
- Mac终端处理MySql
进入数据库: mysql -u root -p 随后输入密码:root 原文出处: GarveyCalvin的博客(@GarveyCalvin) MySQL有很多的可视化管理工具,比如“mysql ...
- php把时间存入数据库为年月日时分秒类型
时间用now() $sql1 = "insert into registerip (name,logintime,ip,url) values ('1211121',now(),'127.0 ...
- EasyDarwin开源流媒体云平台之EasyRMS录播服务器功能设计
需求背景 EasyDarwin开发团队维护EasyDarwin开源流媒体服务器也已经很多年了,之前也陆陆续续尝试过很多种服务端录像的方案,有:在EasyDarwin中直接解析收到的RTP包,重新组包录 ...
- 网络直播流媒体协议的选择讨论,RTSP,RTMP,HTTP,私有协议?
最近有不少人在EasyDarwin的交流群里面问关于花椒.映客手机直播技术的问题,还有RTSP.RTMP协议选择的问题,这里个人谈一下自己的愚见. 1.不管是RTSP/RTP.RTMP.HTTP,亦或 ...
- Dubbo Spring Cloud Motan
跨语言统一治理.Golang,谈谈另辟蹊径的开源RPC框架Motan_搜狐科技_搜狐网 https://www.sohu.com/a/207389967_467759
- J++ C#
J++几乎有与Java相同的编程语言和虚拟机.
- Chapter 3 Shared Assemblies and Strongly Named Assemblies
As time marches on,MS developers and control developer modify their code:they fix bugs,patch securit ...