面试题25二叉树中和为某一值的路径
思路:设立一个vector<int>Path 记录路径,定义一个static int sum 累加和。先序遍历二叉树一直累加根结点到叶子结点的值,当结点是叶子结点同时和为指定值时输出路径
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
//面试题25二叉树中和为某一值的路径 思路:设立一个vector<int>Path 记录路径,定义一个static int sum 累加和。先序遍历二叉树一直累加根结点到叶子结点的值,当结点是叶子结点同时和为指定值时输出路径
typedef struct BinaryTreeNode
{
int data;
BinaryTreeNode *left,*right;
}BinaryTreeNode;
void FindPath(BinaryTreeNode*pRoot,int expectedSum,vector<int>&Path)
{
if(pRoot==NULL)
{
return ;
}
static int sum=0;
sum+=pRoot->data;
Path.push_back(pRoot->data);
bool IsLeaf=(pRoot->left==NULL)&&(pRoot->right==NULL);
if(IsLeaf