四、二叉树中和为某一值的路径
《剑指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);
}
