/*
25:> 二叉树中和为某一定值的路径
递归;栈保留序列(但为了打印完整,使用vector)
*/
void FindPath(BinTree* pRoot, int val, vector<int> path, int cursum)
{
cursum += pRoot->m_nValue;
path.push_back(pRoot->m_nValue);
bool is_Leaf = pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL;
if (cursum == val && is_Leaf)
{
for (vector<int>::iterator _iter = path.begin(); _iter != path.end(); ++_iter)
cout << *_iter << " ";
cout << endl;
}
if (pRoot->m_pLeft)
FindPath(pRoot->m_pLeft, val, path, cursum);
if (pRoot->m_pRight)
FindPath(pRoot->m_pRight, val, path, cursum);
path.pop_back();
}
void FindPath(BinTree* pRoot,int val)
{
if (pRoot == NULL)
return;
vector<int> path;
int cursum = 0;
FindPath(pRoot, val, path, cursum);
}
二叉树中和为某一定值的路径
最新推荐文章于 2021-05-17 12:13:30 发布