手机版
你好,游客 登录 注册
背景:
阅读新闻

二叉树类型笔试面试题大总结(含代码)

[日期:2014-07-30] 来源:Linux社区  作者:xiajun07061225 [字体:大 中 小]

四、二叉树中和为某一值的路径

《剑指Offer》面试题25

同样是递归思想。

 

代码:

void find_path(BinaryTreeNode *root,intexpected_sun){

vector<int>path;

intcur_sum =0;

 

find_path(root,expected_sun,path,cur_sum);

}

 

void find_path(BinaryTreeNode *root,intexpected_sun,vector<int>&path,int cur_sum){

cur_sum +=root->m_nValue;

path.push_back(root->m_nValue);

// 当前节点是叶子节点而且路径上节点值的和满足条件

if(expected_sun == cur_sum && NULL ==root->m_pLeft &&NULL == root->m_pRight)

{

//输出路径

vector<int>::iterator iter=path.begin();

cout << "Path:";

for(;iter != path.end();++iter)

{

cout<< *iter<< "";

}

cout << endl;

}

 

if(root->m_pLeft!=NULL)

{

find_path(root->m_pLeft,expected_sun,path,cur_sum);

}

 

if(root->m_pRight!=NULL)

{

find_path(root->m_pRight,expected_sun,path,cur_sum);

}

 

path.pop_back();

cur_sum -=root->m_nValue;

}

 

五、将二叉搜索树转化为双向链表

思路一:当我们到达某一结点准备调整以该结点为根结点的子树时,先调整其左子树将左子树转换成一个排好序的左子链表,再调整其右子树转换右子链表。最近链接左子链表的最右结点(左子树的最大结点)、当前结点和右子链表的最左结点(右子树的最小结点)。从树的根结点开始递归调整所有结点。

 

代码:

/////////////////////////////////////////////////////////////////////

//

Covert a sub binary - search- tree into a sorteddouble-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 );

 

//Connectthegreatestnodeintheleftsub-treetothecurrentnode

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 );

 

//Connecttheleastnodeintherightsub-treetothe currentnode

if (pRight)

{

pNode-> m_pRight= pRight;

pRight-> m_pLeft= pNode;

}

 

BSTreeNode * pTemp= pNode;

// If the current node is the right child ofits parent,

//returnthe leastnodeinthetreewhoserootisthecurrent node

if (asRight)

{

while (pTemp-> m_pLeft)

pTemp= pTemp-> m_pLeft;

}

 

// If the current node is the left child ofits parent,

//returnthegreatestnodeinthetreewhoserootisthecurrentnode

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 wewant to return the headof the sorteddouble-linked list,

// we set the second parameter to be true

returnConvertNode(pHeadOfTree,true );

}

 

思路二:我们可以中序遍历整棵树。按照这个方式遍历树,比较小的结点先访问。如果我们每访问一个结点,假设之前访问过的结点已经调整成一个排序双向链表,我们再把调整当前结点的指针将其链接到链表的末尾。当所有结点都访问过之后,整棵树也就转换成一个排序双向链表了。

 

代码:

BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree)

{

BinaryTreeNode *pLastNodeInList = NULL;

ConvertNode(pRootOfTree,&pLastNodeInList);

 

//pLastNodeInList指向双向链表的尾结点,

//我们需要返回头结点

BinaryTreeNode *pHeadOfList = pLastNodeInList;

while(pHeadOfList!= NULL&& pHeadOfList->m_pLeft!= NULL)

pHeadOfList= pHeadOfList->m_pLeft;

 

return pHeadOfList;

}

 

voidConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList)

{

if(pNode== NULL)

return;

 

BinaryTreeNode *pCurrent = pNode;

 

if (pCurrent->m_pLeft!= NULL)

ConvertNode(pCurrent->m_pLeft,pLastNodeInList);

 

pCurrent->m_pLeft=*pLastNodeInList;

if(*pLastNodeInList!= NULL)

(*pLastNodeInList)->m_pRight= pCurrent;

 

*pLastNodeInList= pCurrent;

 

if (pCurrent->m_pRight!= NULL)

ConvertNode(pCurrent->m_pRight,pLastNodeInList);

}

linux
相关资讯       二叉树 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款