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

来源:互联网 发布:淘宝开放平台教程 编辑:程序博客网 时间:2024/05/29 03:19
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();}

0 0
原创粉丝点击