【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 ...
随机推荐
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html 上一节中我们通过http协议,采用HttpClient向服务器端a ...
- C语言判断回文数
#include<stdio.h> #include<stdlib.h> int main() { //1.得到这个数字 2.翻转 3.进行比较 4.如果相同 就输出 是 否则 ...
- Nginx+ffmpeg的HLS开源server搭建配置及开发具体解释
本文概述: 至眼下为止.HLS 是移动平台上很重要并十分流行的流媒体传输协议.做移动平台的流媒体开发,不知道它不掌握它 .真是一大遗憾.而HLS的平台搭建有一定的难度,本文针对对该方向有一定了解的朋友 ...
- Hadoop实战-MapReduce之max、min、avg统计(六)
1.数据准备: Mike,35 Steven,40 Ken,28 Cindy,32 2.预期结果 Max 40 Min 28 Avg 33 3.MapReduce代码如下 import ja ...
- 在Qt中使用大漠插件
因工作需要,项目需求(要编写一个营销软件,其中一个功能是控制QQ和微信发送广告消息给指定的联系人或群组, 因为我Windows和逆向水平还不到家,起初的调用Windows API的设计方案不可行,于是 ...
- 【WordSearch】Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Struts action
<action name="KnowledgeBankManageAction_*" class="knowledgeBankManageAction" ...
- vuejs实现折叠面板展开收缩动画
vuejs通过css3实现元素固定高度到auto高度的动画和auto高度到固定高度的动画. 循环列表,html: <template> <div class="newsli ...
- NOIP2010_T4_引水入城 bfs+贪心
在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 N 行 M 列的矩形,如上图所示,其中每个格子都代表一座城 市,每座城市都有一个海拔高度.为了使 ...
- 2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分
题目链接:http://codeforces.com/problemset/gymProblem/101028/I I. March Rain time limit per test 2 second ...