您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > 程序员面试精选100题
(01)-把二元查找树转变成排序的双向链表........................................................................1(02)-设计包含min函数的栈................................................................................................5(03)-求子数组的最大和........................................................................................................8(04)-在二元树中找出和为某一值的所有路径..................................................................10(05)-查找最小的k个元素..................................................................................................11(06)-判断整数序列是不是二元查找树的后序遍历结果..................................................13(07)-翻转句子中单词的顺序..............................................................................................15(08)-求1+2+...+n.................................................................................................................17(09)-查找链表中倒数第k个结点......................................................................................20(10)-在排序数组中查找和为给定值的两个数字..............................................................23(11)-求二元查找树的镜像..................................................................................................25(12)-从上往下遍历二元树..................................................................................................28(13)-第一个只出现一次的字符..........................................................................................30(14)-圆圈中最后剩下的数字..............................................................................................33(15)-含有指针成员的类的拷贝..........................................................................................37(16)-O(logn)求Fibonacci数列............................................................................................40(17)-把字符串转换成整数..................................................................................................45(18)-用两个栈实现队列......................................................................................................47(19)-反转链表......................................................................................................................50(20)-最长公共子串(太难懂)..........................................................................................51(21)-左旋转字符串(精彩)..............................................................................................56(22)-整数的二进制表示中1的个数..................................................................................57(23)-跳台阶问题..................................................................................................................60(24)-栈的push、pop序列...................................................................................................60(25)-在从1到n的正数中1出现的次数(不懂).............................................................63(26)-和为n连续正数序列(不错).....................................................................................67(27)-二元树的深度.................................................................................................................70(28)-字符串的排列(暂时还不懂).....................................................................................72(29)-调整数组顺序使奇数位于偶数前面.............................................................................74(30)-异常安全的赋值运算符重载函数.................................................................................77(01)-把二元查找树转变成排序的双向链表题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。比如将二元查找树10/\614/\/\481216转换成双向链表4=6=8=10=12=14=16。分析:本题是微软的面试题。很多与树相关的题目都是用递归的思路来解决,本题也不例外。下面我们用两种不同的递归思路来分析。思路一:当我们到达某一结点准备调整以该结点为根结点的子树时,先调整其左子树将左子树转换成一个排好序的左子链表,再调整其右子树转换右子链表。最近链接左子链表的最右结点(左子树的最大结点)、当前结点和右子链表的最左结点(右子树的最小结点)。从树的根结点开始递归调整所有结点。思路二:我们可以中序遍历整棵树。按照这个方式遍历树,比较小的结点先访问。如果我们每访问一个结点,假设之前访问过的结点已经调整成一个排序双向链表,我们再把调整当前结点的指针将其链接到链表的末尾。当所有结点都访问过之后,整棵树也就转换成一个排序双向链表了。参考代码:首先我们定义二元查找树结点的数据结构如下:structBSTreeNode//anodeinthebinarysearchtree{intm_nValue;//valueofnodeBSTreeNode*m_pLeft;//leftchildofnodeBSTreeNode*m_pRight;//rightchildofnode};思路一对应的代码://///////////////////////////////////////////////////////////////////////Covertasubbinary-search-treeintoasorteddouble-linkedlist//Input:pNode-theheadofthesubtree//asRight-whetherpNodeistherightchildofitsparent//Output:ifasRightistrue,returntheleastnodeinthesub-tree//elsereturnthegreatestnodeinthesub-tree///////////////////////////////////////////////////////////////////////BSTreeNode*ConvertNode(BSTreeNode*pNode,boolasRight){if(!pNode)returnNULL;BSTreeNode*pLeft=NULL;BSTreeNode*pRight=NULL;//Converttheleftsub-treeif(pNode-m_pLeft)pLeft=ConvertNode(pNode-m_pLeft,false);//Connectthegreatestnodeintheleftsub-treetothecurrentnodeif(pLeft){pLeft-m_pRight=pNode;pNode-m_pLeft=pLeft;}//Converttherightsub-treeif(pNode-m_pRight)pRight=ConvertNode(pNode-m_pRight,true);//Connecttheleastnodeintherightsub-treetothecurrentnodeif(pRight){pNode-m_pRight=pRight;pRight-m_pLeft=pNode;}BSTreeNode*pTemp=pNode;//Ifthecurrentnodeistherightchildofitsparent,//returntheleastnodeinthetreewhoserootisthecurrentnodeif(asRight){while(pTemp-m_pLeft)pTem
本文标题:程序员面试精选100题
链接地址:https://www.777doc.com/doc-6312601 .html