【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 ...
随机推荐
- VS快速生成JSON数据类
1.复制Json数据 2.
- Python实战之自己主动化评论
Python实战之自己主动化评论 玩csdn博客一个多月了,渐渐发现了一些有意思的事,常常会有人用相同的评论到处刷.不知道是为了加没什么用的积分,还是纯粹为了表达楼主好人.那么问题来了,这种无聊的事情 ...
- 用python编写的无线AP扫描器
代码如下: #coding=utf-8 import os import sys import subprocess from scapy.all import * RSN = 48 #管理帧信息元素 ...
- Appium python自动化测试系列之认识Appium(四)
4.1界面认识 在之前安装appium的时候说过我们有两种方法安装,也就有两种结果,一种是有界面的,一种是没有界面的,首先我们先讲一下有界面的,以及界面有哪些东西. 首先看第一幅图,如果你的是win ...
- CAN协议与CANOpen协议
这里详细介绍了CAN协议中数据通信帧每位的含义,有图片,值得一看:https://www.cnblogs.com/pejoicen/p/3986587.html 这里介绍了CanOpen协议,http ...
- linux 网络设备,网卡配置 ,相关
网络设备,网卡配置: Eth0是物理网卡:唯一mac地址,Bcast:广播地址,MAsk:子网掩码, Lo:系统自带的回环的ip地址,可以做一些基本的测试应用,比如没有网卡就用127.0.0.1, r ...
- [haoi2014]穿越封锁线
这题需要注意的一点是射线法需要考虑边界,而且题目对边界的限制极为严格. dcmp(v[i%n].x-x)<=0&&dcmp(v[(i+1)%n].x-x)>0 dcmp(v ...
- [noip2014day2-T1]无线网路发射器选址
随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129条东西向街道和129条南北向街道所形成的网格状,并且相邻的平行街道之间 ...
- Java 递归算法,遍历文件夹下的所有文件。
用递归算法遍历文件下的所有子文件夹和子文件 文件夹遍历方法 public void getFileList(String strPath){ File f=new File(strPath); try ...
- hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
#1103 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorit ...