/** * 打印从根到叶子各个节点和为iSum的路径 * @param pNode 树根节点(该函数不负责判空操作) * @param iSum 所要查找的路径和 * @param path 路径(数组长度为树的深度,可用容器替代) * @param size 路径长度(初始值为0) */ void FindPath(BTNode *pNode, int iSum, int path[], int size) { if(NULL == pNode) { int sum = 0; for(int i = 0; i < size; ++i) { sum += path[i]; } if(sum == iSum) { for (int i = 0; i < size; ++i) { cout << path[i] << " "; } cout << endl; } else { return; } } path[size] = pNode->m_iValue; FindPath(pNode->m_pLeft, iSum, path, size + 1); FindPath(pNode->m_pRight, iSum, path, size + 1); }