求二叉树中和为某一值的路径

struct BinaryTreeNode{
	int m_nValue;
	BinaryTreeNode *m_pLeft;
	BinaryTreeNode *m_pRight;
};

void FindPath(BinaryTreeNode *pRoot, int expectedSum, vector<int> &path, int &currentSum){
	if(!pRoot)
		return;
	currentSum += pRoot->m_nValue;
	path.push_back(pRoot);
	bool isLeaf = pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL;
	if(currentSum == expectedSum && isLeaf){
		for(vector<int>::iterator i = path.begin(); i != path.end(); ++i)
			printf("%d ", *i);
		printf("\n");
	}
	FindPath(pRoot->m_pLeft, expectedSum, path, currentSum);
	FindPath(pRoot->m_pRight, expectedSum, path, currentSum);
	currentSum -= pRoot->m_nValue;
	path.pop_back();
}

你可能感兴趣的:(求二叉树中和为某一值的路径)