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