首页 > 全部 > 程序员面试 > 程序员面试模拟试卷6

程序员面试模拟试卷6

本单篇文档共21061字,内容预览3600字,预览为有答案版,源文件无水印,下载后包含无答案空白卷版和有答案版,同时也有计算机类整科真题模拟题,讲义课件,思维导图,易错高频题等下载。
程序员面试 模拟试卷 3477人下载
价格: 0.80 原价:¥9.00
收藏

程序员面试模拟试卷6

面试题

1.输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。

  比如将二元查找树

首先我们定义二元查找树结点的数据结构如下:

struct BSTreeNode // a node in the binary search tree

{

int m_nValue; // value of node

BSTreeNode *m_pLeft; // left child of node

BSTreeNode *m_pRight; // right child of node

};

思路一对应的代码:

///////////////////////////////////////////////////////////////////////

// Covert a sub binary-search-tree into a sorted double-linked list

// Input: pNode – the head of the sub tree

// asRight – whether pNode is the right child of its parent

// Output: if asRight is true, return the least node in the sub-tree

// else return the greatest node in the sub-tree

///////////////////////////////////////////////////////////////////////

BSTreeNode* ConvertNode(BSTreeNode* pNode, bool asRight)

{

if(!pNode)

return NULL;

BSTreeNode *pLeft = NULL;

BSTreeNode *pRight = NULL;

// Convert the left sub-tree

if(pNode->m_pLeft)

pLeft = ConvertNode(pNode->m_pLeft, false);

// Connect the greatest node in the left sub-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 right sub-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 of its 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 of its 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;

}

///////////////////////////////////////////////////////////////////////

// Covert a binary search tree into a sorted double-linked list

// Input: the head of tree

// Output: the head of sorted double-linked list

///////////////////////////////////////////////////////////////////////

BSTreeNode* Convert(BSTreeNode* pHeadOfTree)

{

// As we want to return the head of the sorted double-linked list,

// we set the second parameter to be true

return ConvertNode(pHeadOfTree, true);

}

思路二对应的代码:

///////////////////////////////////////////////////////////////////////

// Covert a sub binary-search-tree into a sorted double-linked list

// Input: pNode – the head of the sub tree

// pLastNodeInList – the tail of the double-linked list

///////////////////////////////////////////////////////////////////////

void ConvertNode(BSTreeNode* pNode, BSTreeNode*& pLastNodeInList)

{

if(pNode == NULL)

return;

BSTreeNode *pCurrent = pNode;

<

本文档预览:3600字符,共21061字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载

剩余未完,查看全文
收藏
程序员面试模拟试卷6

推荐资源

客服

扫码添加客服微信

热线

官方客服

如遇问题,请联系客服为您解决

电话客服:

客服微信:pujinet

工作时间:9:00-18:00,节假日休息

公众号

扫码关注微信公众号