笔试面试之将2叉查找树转换为双向链表
2010-06-18 21:09
302 查看
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。
比如将二元查找树
10
/
/
6
14
/ / /
/
4 8 12 16
转换成双向链表
4=6=8=10=12=14=16。
分析:本题是微软的面试题。很多与树相关的题目都是用递归的思路来解决,本题也不例外。下面我们用两种不同的递归思路来分析。
思路一:当我们到达某一结点准备调整以该结点为根结点的子树时,先调整其左子树将左子树转换成一个排好序的左子链表,再调整其右子树转换右子链表。最近链接左子链表的最右结点(左子树的最大结点)、当前结点和右子链表的最左结点(右子树的最小结点)。从树的根结点开始递归调整所有结点。
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);
}
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);
}
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;
}
转载自:http://zhedahht.blog.163.com/blog/static/254111742007127104759245/
比如将二元查找树
10
/
/
6
14
/ / /
/
4 8 12 16
转换成双向链表
4=6=8=10=12=14=16。
分析:本题是微软的面试题。很多与树相关的题目都是用递归的思路来解决,本题也不例外。下面我们用两种不同的递归思路来分析。
思路一:当我们到达某一结点准备调整以该结点为根结点的子树时,先调整其左子树将左子树转换成一个排好序的左子链表,再调整其右子树转换右子链表。最近链接左子链表的最右结点(左子树的最大结点)、当前结点和右子链表的最左结点(右子树的最小结点)。从树的根结点开始递归调整所有结点。
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);
}
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);
}
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;
}
转载自:http://zhedahht.blog.163.com/blog/static/254111742007127104759245/
相关文章推荐
- 笔试面试之判断序列是否2叉查找树后序遍历
- 笔试面试算法经典--二叉搜索树转有序的双向链表(Java)
- 面试OR笔试40——二分查找一个函数解决多个问题
- C/C++ 笔试面试(2)——二分查找 的确遇到的笔试题
- 笔试算法题(05):转换BST为双向链表 & 查找栈中的最小元素
- C/C++ 笔试面试(2)——二分查找
- 面试100题系列之1将查找二叉树转换成有序的双向链表
- 微软100题 第一题(查找树转换为有序双向链表)
- C/C++ 笔试面试(2)——二分查找
- 笔试面试之字符串转换为整数
- 面试笔试(一):平衡二叉树,Java正则验证,List查找时间复杂度,ORM框架的选择
- 链表节点面试100题系列之1将查找二叉树转换成有序的双向链表
- 笔试面试之有序数组中查找和为定值的两个数
- 笔试面试之查找最小的k个数
- 32个经典的Java面试笔试题
- java面试笔试题整理
- memcopy和memmove的区别(笔试,面试)
- 笔试面试失败总结
- C++笔试面试常考知识点汇总(三)
- 程序员笔试面试要点