void FindPath1(BinaryTreeNode* pRoot, int sum, std::vector<int>&path, int currentSum);
void FindPath(BinaryTreeNode* root,int sum)
{
if (root == NULL)
{
return;
}
std::vector<int> path;
int currentSum = 0;
FindPath1(root,sum,path,currentSum);
}
void FindPath1(BinaryTreeNode* pRoot, int sum, std::vector<int>&path, int currentSum)
{
currentSum += pRoot->m_value;
path.push_back(pRoot->m_value);
bool isLeaf = pRoot->m_pLeft == NULL&&pRoot->m_pRight == NULL;
if (currentSum == sum&&isLeaf)
{
printf("A path is find:");
std::vector<int>::iterator iter = path.begin();
for (; iter != path.end(); iter++)
{
printf("%d\t", &iter);
}
printf("\n");
}
if (pRoot->m_pLeft != NULL)
{
FindPath1(pRoot->m_pLeft,sum,path,currentSum);
}
if (pRoot->m_pRight != NULL)
{
FindPath1(pRoot->m_pRight,sum,path,currentSum);
}
currentSum -= pRoot->m_value;
path.pop_back();
}
06-13
06-13
06-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交