二叉树路径和等于某个值

struct BtreeNode

{

int val;

BtreeNode*pleft;

BtreeNode*pright;

}

void findpath(BtreeNode*root,int target)

{

if(!root)

{

return;

}

vector<int>path;

int cursum=0;

void findpath(root,path,cursum,target);

}

findpath(BtreeNode*root,vector<int>path,int cursum,int target)

{

cursum+=root->val;

path.push_back(root->val);

if(cursum==target&&root->left=root->right==NULL)

{

cout<<"find path"<<endl;

for(int i=0;i<path.size();i++)

{

cout<<path[i]<<endl;

}

}

if(!root->left)

{

findpath(root->left,path,cursum,target);

}

if(!root->right)

{

findpath(root->right,path,cursum,target);

}

path.pop_back();

cursum-=root->val;

};

原文地址:https://www.cnblogs.com/mmziscoming/p/5811165.html