#include "iostream"
#include "iomanip"
#include "stack"
#include "queue"
#include "vector"
using namespace std;
struct BinaryTreeNode{
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
};
void findPath(BinaryTreeNode *root, int expectValue)
{
if (root == NULL)
return;
vector<int> path;
int currentSum = 0;
findPath(root, expectValue, path, currentSum);
}
void findPath(BinaryTreeNode *root, int expectValue,vector<int> &path,int ¤tSum)
{
currentSum += root->data;
path.push_back(root->data);
bool isLeaf = root->left == NULL&&root->right == NULL;
if (isLeaf&&expectValue==currentSum)
{
cout << "a path: ";
vector<int>::iterator iter = path.begin();
for (; iter != path.end();++iter)
{
cout << *iter<<" ";
}
cout << endl;
}
if (root->left != NULL)
findPath(root->left, expectValue, path, currentSum);
if (root->right!=NULL)
findPath(root->right, expectValue, path, currentSum);
currentSum -= root->data;
path.pop_back();
}
int main()
{
system("pause");
return 0;
}
剑指offer面试题25:二叉树中和为某一值的路径
最新推荐文章于 2024-05-28 15:49:16 发布