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

/* 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);
}

你可能感兴趣的:(二叉树中和为某一定值的路径)