# 得到二叉树中从根节点到树中某一节点的路径

1703人阅读 评论(0)

<span style="font-size:14px;">#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct BtNode
{
int value;
struct BtNode *lchild;
struct BtNode *rchild;
}BtNode, *Bitree;

Bitree newBtNode()
{
Bitree p = new BtNode;

if( NULL == p )
{
cout << "newBtNode func: err -1, NULL==p " << endl;
return p;
}

p->value = 0;
p->lchild = p->rchild = NULL;
return p;
}

{
int ret = 0;

{
cout << "createBitree func: err -1, NULL==head" << endl;
ret = -1;
return ret;
}

int data = 0;
cin >> data;

if( 0 == data ) // leaf
{
}
else
{
}

return ret;

}

{
{
cout << head->value << ' ';
}

return;
}

{
{
cout << head->value << ' ';
}

return;
}

{
{
cout << head->value << ' ';
}

return;
}

// 在二叉树(并不需要是排序二叉树)中查找value的节点，并返回指向该节点的指针，没找到返回NULL
Bitree getNodeByValue( Bitree root, int value )
{
Bitree pRet = NULL;

if( NULL == root )
{
cout << "getNodeByValue func: err -1, NULL==root" << endl;
return pRet;
}

if( root->value == value )
{
pRet = root;
return pRet;
}
else
{
Bitree pLeftResult = NULL;
Bitree pRightResult = NULL;

if( root->lchild != NULL )
{
pLeftResult = getNodeByValue( root->lchild, value );
}

if( pLeftResult != NULL ) // 找到了
{
pRet = pLeftResult;
return pRet;
}

if( root->rchild != NULL )
{
pRightResult = getNodeByValue( root->rchild, value );
}

if( pRightResult != NULL ) // 找到了
{
pRet = pRightResult;
return pRet;
}
else
{
return pRet;
}

}

}

// 查找从根节点到pNode节点的路径
vector<Bitree> getNodePathFromRoot( Bitree root, Bitree pNode, vector<Bitree> curPath )
{
vector<Bitree> path;

if( NULL == root || NULL == pNode )
{
cout << "getNodePathFromRoot func: err -1, NULL == root || NULL == pNode" << endl;
return path;
}

if( root == pNode )
{
curPath.push_back( root );
return curPath;
}

Bitree pleft = root->lchild;
Bitree pright = root->rchild;
vector<Bitree> leftResult;
vector<Bitree> rightResult;

curPath.push_back( root );

if( pleft != NULL )
{
leftResult = getNodePathFromRoot( pleft, pNode, curPath );
}

if( pright != NULL )
{
rightResult = getNodePathFromRoot( pright, pNode, curPath );
}

if( leftResult.size() > 0 && leftResult[ leftResult.size() - 1 ] == pNode )
{
//cout << "find in leftTree" << endl;
return leftResult;
}

if( rightResult.size() > 0 && rightResult[ rightResult.size() - 1 ] == pNode )
{
//cout << "find in rightTree" << endl;
return rightResult;
}

return path;
}

//查找从根节点到pNode节点的路径
//返回0表示查找成功，-1表示查找失败
int getNodePathFromRoot1( Bitree root, Bitree pNode, vector<Bitree> &path )
{
int ret = 0;

if( NULL == root || NULL == pNode )
{
cout << "getNodePathFromRoot1 func: err -1, NULL == root || NULL == pNode" << endl;
ret = -1;
return ret;
}

if( root == pNode ) // 查找到了
{
path.push_back(root);
return ret;
}

path.push_back(root);

Bitree pleft = root->lchild;
Bitree pright = root->rchild;

int found = -1;

if( pleft != NULL )
{
int ret1 = getNodePathFromRoot1( pleft, pNode, path ); // 从左子树查找

if( 0 == ret1 )
{
ret = ret1;
return ret;
}
}

if( pright != NULL )
{
int ret2 = getNodePathFromRoot1( pright, pNode, path ); // 从右子树查找

if( 0 == ret2 )
{
ret = ret2;
return ret;
}
}

path.pop_back(); // 左右子树都没有找到就弹出当前元素
ret = -1;
return ret; // 返回查找失败的标志

}

int main()
{
int ret = 0;

Bitree root = NULL;
createBitree( &root );

preTravel(root);
cout << endl;

midTravel(root);
cout << endl;

postTravel(root);
cout << endl;

cout << "============================" << endl << endl;

int value1 = 0;
int value2 = 0;
Bitree pFirstNode = NULL;

while( cin >> value1 )
{
Bitree pNode = getNodeByValue( root, value1 );
vector<Bitree> curPath;
//vector<Bitree> path = getNodePathFromRoot( root, pNode, curPath );
getNodePathFromRoot1( root, pNode, curPath );

int len = curPath.size();

for( int i = 0; i < len; ++i )
{
cout << curPath[i]->value << ' ';
}
cout << endl;
}
return ret;
}
// 这里是排序二叉树，其实并不要求是排序二叉树，普通的二叉树也行，也可以推广到普通的树中
/*

10
/  \
5    15
/\     /\
3  8   12 16
/   /\
1  6  9

./a.out
10 5 3 1 0 0 0 8 6 0 0 9 0 0 15 12 0 0 16 0 0
10 5 3 1 8 6 9 15 12 16
1 3 5 6 8 9 10 12 15 16
1 3 6 9 8 5 12 16 15 10
============================

3
10 5 3
15
10 15
9
10 5 8 9
^C

</span>

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：521581次
• 积分：4588
• 等级：
• 排名：第6383名
• 原创：95篇
• 转载：115篇
• 译文：0篇
• 评论：27条
评论排行
最新评论