首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。 比如将二元查找树 10
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。 比如将二元查找树 10
admin
2014-11-15
110
问题
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。
比如将二元查找树
10
/ \
6 14
/ \ / \
4 8 12 16
转换成双向链表
4=6=8=10=12=14=16。
选项
答案
思路一:当我们到达某一结点准备调整以该结点为根结点的子树时,先调整其左子树将左子树转换成一个排好序的左子链表,再调整其右子树转换右子链表。最近链接左子链表的最右结点(左子树的最大结点)、当前结点和右子链表的最左结点(右子树的最小结点)。从树的根结点开始递归调整所有结点。 思路二:我们可以中序遍历整棵树。按照这个方式遍历树,比较小的结点先访问。如果我们每访问一个结点,假设之前访问过的结点已经调整成一个排序双向链表,我们再把调整当前结点的指针将其链接到链表的末尾。当所有结点都访问过之后,整棵树也就转换成一个排序双向链表了。 参考代码: 首先我们定义二元查找树结点的数据结构如下: 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; // 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 = pCurrent; // 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(BSTreeNode* pHeadOfTree) { BSTreeNode *pLastNodeInList = NULL; ConvertNode(pHeadOfTree, pLastNodeInList); // Get the head of the double-linked list BSTreeNode *pHeadOfList = pLastNodeInList; while(pHeadOfList && pHeadOfList->m_pLeft) pHeadOfList = pHeadOfList->m_pLeft; return pHeadOfList; }
解析
转载请注明原文地址:https://kaotiyun.com/show/7DmZ777K
0
程序员面试
相关试题推荐
"Thecatdoesnotofferservices,"WilliamBurroughswrote."Thecatoffersitself."Butitdoessowithunapologeticcontradict
大概描述一下ASP。NET页面的生命周期
设置TCP/IP属性的首选DNS服务器地址202.112.80.106。
请打开"计算器"应用程序,利用科学型模式将十六进制的ABC转换为二进制。
若按下Shift键同时用鼠标分别单击幻灯片中的标题、文本和图片对象,则()。A.仅最后单击的对象被选中B.三个对象被同时选中C.仅标题对象被选中D.仅图形对象被选中
以下关于输入法状态切换的组合键正确的是______A.使用Ctrl+、来切换中文标点和英文标点B.使用Ctrl+空格键来打开或关闭中文输入法C.使用Shift+空格键来切换半角输入模式和全角输入模式D.使用Shift+Ctrl来切换半角输入模式和
当密码为123456时,请撤消对工作簿Book1的密码及结构保护设置。
请对工作簿Book1设置密码123456,同时对其结构进行保护。
信息安全是一门以人为主,涉及技术、管理和法律的综合学科,同时还与个人道德意识等方面紧密相关。
信息安全包括四大要素:技术、制度、流程和()。
随机试题
在“Acc2.mdb”数据库中有“入学登记”表、“系”和“专业”表。(1)以“入学登记”、“系”和“专业”表为数据源,创建生成表查询“查询1”,生成“入学明细”表,生成“ID”、“姓名”、“性别”、“出生年月日”、“高考所在地”、“高考分数”、“专业名称
四气的确定是
甲公司将两个业务部门分出设立乙公司和丙公司,并在公司分立决议中明确,甲公司以前所负的债务由新设的乙公司承担。分立前甲公司欠丁企业货款12万元,现丁企业要求偿还。根据《合同法》的规定,下列关于该12万元债务承担的表述中,正确的是()。
根据现行税务行政处罚规定,下列属于税务行政处罚的有()。
国际旅行社的注册资本不得少于()元人民币。
长期以来,中国文学被人为地分成古代、现代两大板块,这固然可以使文学研究更加专门化,但却造成了现代文学和古代文学研究的________,在大多数学者眼里,中国现代文学是以现代性追求呈现出新的文学特质,与西方文学的因缘更密切,而对中国古代文学却________
法律援助是指在国家设立的法律授助机构的指导和协调下,律师、公证人员、基层法律工作者等法律服务人员依法为经济困难或特殊案件的当事人给予减、免收费提供法律帮助的一项法律制度。它有一般对象和特殊对象,前者是要审查其是否是具备援助条件的中国公民;后者是指符合刑事诉
Priortothe20thcentury,manylanguageswithsmallnumbersofspeakerssurvivedforcenturies.Theincreasinglyinterconnected
农业革命
我国刑法的基本原则之一是()。
最新回复
(
0
)