struct BinaryTreeNode{
int m_nValue;
BinaryTreeNode *m_pLeft;
BinaryTreeNode *m_pRight;
};
void FindPath(BinaryTreeNode *pRoot, int expectedSum, vector<int> &path, int ¤tSum){
if(!pRoot)
return;
currentSum += pRoot->m_nValue;
path.push_back(pRoot);
bool isLeaf = pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL;
if(currentSum == expectedSum && isLeaf){
for(vector<int>::iterator i = path.begin(); i != path.end(); ++i)
printf("%d ", *i);
printf("\n");
}
FindPath(pRoot->m_pLeft, expectedSum, path, currentSum);
FindPath(pRoot->m_pRight, expectedSum, path, currentSum);
currentSum -= pRoot->m_nValue;
path.pop_back();
}
求二叉树中和为某一值的路径
最新推荐文章于 2023-05-11 23:34:12 发布