面试题27二叉搜索树与双向链表

题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。


比如输入图4.12 中左边的二叉搜索树,则输出转换之后的排序现向链表。

这里写图片描述

结点定义:

public static class BinaryTreeNode {
    int value;
    BinaryTreeNode left;
    BinaryTreeNode right;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

解题思路:

在二叉树中,每个结点都有两个指向子结点的指针。在双向链表中,每个结点也有两个指针,它们分别指向前一个结点和后一个结点。由于这两种结点的结构相似,同时二叉搜索树也是一种排序的数据结构,因此在理论上有可能实现二叉搜索树和排序的双向链表的转换。在搜索二叉树中,左子结点的值总是小于父结点的值,右子结点的值总是大于父结点的值。因此我们在转换成排序双向链表时,原先指向左子结点的指针调整为链表中指向前一个结点的指针,原先指向右子结点的指针调整为链表中指向后一个结点指针。接下来我们考虑该如何转换。

由于要求转换之后的链表是排好序的,我们可以中序遍历树中的每一个结点, 这是因为中序遍历算法的特点是按照从小到大的顺序遍历二叉树的每一个结点。当遍历到根结点的时候,我们把树看成三部分:值为10 的结点、根结点值为6 的左子树、根结点值为1 4 的右子树。根据排序链表的定义,值为10 的结点将和它的左子树的最大一个结点(即值为8 的结点)链接起来,同时它还将和右子树最小的结点(即值为12 的结点)链接起来,如图4.13 所示。

这里写图片描述

按照中序遍历的顺序, 当我们遍历转换到根结点(值为10 的结点)时,它的左子树已经转换成一个排序的链表了, 并且处在链表中的最后一个结点是当前值最大的结点。我们把值为8 的结点和根结点链接起来,此时链表中的最后一个结点就是10 了。接着我们去地历转换右子树, 并把根结点和右子树中最小的结点链接起来。至于怎么去转换它的左子树和右子树,由于遍历和转换过程是一样的,我们很自然地想到可以用递归。

#include<iostream>
#include<stdlib.h>

using namespace std;
struct BinaryTreeNode
{
	int _val;
	BinaryTreeNode* _left;
	BinaryTreeNode* _right;
};
BinaryTreeNode* CreateBinaryTreeNode(int val)
{
	BinaryTreeNode* Node=new BinaryTreeNode();
	Node->_val=val;
	Node->_left=NULL;
	Node->_right=NULL;
	return Node;
}
void ConnectTreeNodes(BinaryTreeNode* parent,BinaryTreeNode* left,BinaryTreeNode* right)
{
	if(parent!=NULL)
	{
		parent->_left=left;
		parent->_right=right;
	}
}
void in(BinaryTreeNode* Root)
{
	if(Root==NULL)
		return;
	in(Root->_left);
	cout<<Root->_val<<" ";
	in(Root->_right);
}
void convertnode(BinaryTreeNode* node,BinaryTreeNode** lastlist)
{
	if(node==NULL)
		return;
	BinaryTreeNode* cur=node;
	if(cur->_left!=NULL)
		convertnode(cur->_left,lastlist);
	cur->_left=*lastlist;
	if((*lastlist)!=NULL)
		(*lastlist)->_right=cur;
	*lastlist=cur;

	if(cur->_right!=NULL)
		convertnode(cur->_right,lastlist);
}
BinaryTreeNode* Convert(BinaryTreeNode* root)
{
	BinaryTreeNode* lastlist=NULL;
	convertnode(root,&lastlist);

	//求链表头结点
	BinaryTreeNode* Head=lastlist;
	while(Head!=NULL&&Head->_left!=NULL)
		Head=Head->_left;
	return Head;
}
void PrintList(BinaryTreeNode* root)
{
	BinaryTreeNode* node=root;
	while(node!=NULL)
	{
		cout<<node->_val<<" ";
		node=node->_right;
	}
	cout<<endl;
}
int main()
{
//            10
//         /      \
//        6        14
//       /\        /\
//      4  8     12  16
    //创建树结点
    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);

    //PrintTree(pNode10);
    in(pNode10);//中序遍历
	cout<<endl;
    BinaryTreeNode* pHeadOfList=Convert(pNode10);//获取双向链表头结点
    PrintList(pHeadOfList);//输出链表

    system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值