[计算机类试卷]程序员面试模拟试卷2及答案与解析.doc
《[计算机类试卷]程序员面试模拟试卷2及答案与解析.doc》由会员分享,可在线阅读,更多相关《[计算机类试卷]程序员面试模拟试卷2及答案与解析.doc(11页珍藏版)》请在麦多课文档分享上搜索。
1、程序员面试模拟试卷 2及答案与解析 1 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。 比如将二元查找树 10 / 6 14 / / 4 8 12 16 转换成双向链表 4=6=8=10=12=14=16。 2 定义栈的数据结构,要求添加一个 min函数,能够得到栈的最小元素。要求函数 min、 push以及 pop的时间复杂度都是 O(1)。 3 输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为 O(n)。 例如输入的数组为 1, -2,
2、 3, 10, -4, 7, 2, -5,和最大的子数组为 3, 10, -4, 7, 2,因此输出为该子数组的和 18。 4 输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。 例如输入整数 22和如下二元树 10 / 5 12 / 4 7 则打印出两条路径: 10, 12和 10, 5, 7。 二元树结点的数据结构定义为: struct BinaryTreeNode / a node in the binary tree int m_nValue; / value of node BinaryTreeNode *m
3、_pLeft; / left child of node BinaryTreeNode *m_pRight; / right child of node ; 程序员面试模拟试卷 2答案与解析 1 【正确答案】 思路一:当我们到达某一结点准备调整以该结点为根结点的子树时,先调整其左子树将左子树转换成一个排好序的左子链表,再调整其右子树转换右子链表。最近链接左子链表的最右结点(左子树的最大结点)、当前结点和右子链表的最左结点(右子树的最小结点)。从树的根结点开始递归调 整所有结点。 思路二:我们可以中序遍历整棵树。按照这个方式遍历树,比较小的结点先访问。如果我们每访问一个结点,假设之前访问过的结点
4、已经调整成一个排序双向链表,我们再把调整当前结点的指针将其链接到链表的末尾。当所有结点都访问过之后,整棵树也就转换成一个排序双向链表了。 参考代码: 首先我们定义二元查找树结点的数据结构如下: 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 binar
5、y-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
6、* 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_
7、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 chil
8、d 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 =
9、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 secon
10、d 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* BSTreeNode *
11、pCurrent = pNode; / Convert the left sub-tree if (pCurrent-m_pLeft != NULL) ConvertNode(pCurrent-m_pLeft, pLastNodeInList); / Put the current node into the double-linked list pCurrent-m_pLeft = pLastNodeInList; if(pLastNodeInList != NULL) pLastNodeInList-m_pRight = pCurrent; pLastNodeInList = pCurre
12、nt; / Convert the right sub-tree if (pCurrent-m_pRight != NULL) ConvertNode(pCurrent-m_pRight, pLastNodeInList); / / Covert a binary search tree into a sorted double-linked list / Input: pHeadOfTree - the head of tree / Output: the head of sorted double-linked list / BSTreeNode* Convert_Solution1(BS
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 试卷 程序员 面试 模拟 答案 解析 DOC
