C语言 打印路径节点值的和为指定和的所有路径

typedef struct treeNode{
	int data;
	struct treeNode *pLeft;
	struct treeNode *pRight;
}treeNode;

void printAllPathWithSum(treeNode *pRoot, int sum, list<treeNode *>&path, int ¤tSum){
	if(pRoot == NULL){
		return;
	}
	currentSum += pRoot->data;
	path.push_back(pRoot);
	if(currentSum == sum && pRoot->pLeft == NULL && pRoot->pRight == NULL){
		list<treeNode *>::iterator iter = path.begin();
		while(iter != path.end()){
			treeNode * pNode = *iter;
			printf("%d ", pNode->data);
			iter++;
		}
		printf("\n");
	}
	if(pRoot->pLeft != NULL){
		printAllPathWithSum(pRoot->pLeft, sum, path, currentSum);
	}
	if(pRoot->pRight != NULL){
		printAllPathWithSum(pRoot->pRight, sum, path, currentSum);
	}
	currentSum -= pRoot->data;
	path.pop_back();
}

点赞