面试题25:二叉树中和为某一值的路径

来源:互联网 发布:extjs表单设计器源码 编辑:程序博客网 时间:2024/06/03 20:44
1.输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径,从树的根节点开始往下一直到叶节点所经过的节点所形成的路径。

例如输入图中的二叉树和一个整数值22,会有两条路径,第一条路径10,12,另一条路径10,5,7.


分析过程如下:


通过上面表格的分析可以发现,用前序遍历访问树的节点时,应该将节点添加到路径上,并累加该节点的值,如果该节点为叶节点并且路径中的节点的值的和刚好等于输入的整数,则符合当前的要去,可以打印出来。如果不是叶节点的话,则继续访问它的子节点,当前节点访问结束时,递归函数将自动的回到它的父节点。因此在函数退出时应该在路径上删除当前的的节点并减去当当前节点的值,确保返回的是从根节点到父节点的路径。保存路径的数据结构可以用栈来实现。
源码:
/*** 功能说明:二叉树的路径* 作者:K0713* 日期:2016-7-16**/#include<iostream>#include<vector>using namespace std;//二叉树结构struct BinaryTreeNode{int                    m_nValue;BinaryTreeNode*        m_pLeft;BinaryTreeNode*        m_pRight;};//创建树结点BinaryTreeNode* CreateBinaryTreeNode(int value){BinaryTreeNode* pNode = new BinaryTreeNode();pNode->m_nValue = value;pNode->m_pLeft = NULL;pNode->m_pRight = NULL;return pNode;}//连接树结点void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight){if (pParent != NULL){pParent->m_pLeft = pLeft;pParent->m_pRight = pRight;}}//打印void PrintTreeNode(BinaryTreeNode* pNode){if (pNode != NULL){cout << "value of this node is: " << pNode->m_nValue << endl;if (pNode->m_pLeft != NULL)cout << "value of its left child is: " << pNode->m_pLeft->m_nValue << endl;elsecout << "left child is null.\n";if (pNode->m_pRight != NULL)cout << "value of its right child is:" << pNode->m_pRight->m_nValue << endl;elsecout << "right child is null.\n";}else{cout << "this node is null.\n";}cout << endl;}void PrintTree(BinaryTreeNode* pRoot){PrintTreeNode(pRoot);if (pRoot != NULL){if (pRoot->m_pLeft != NULL)PrintTree(pRoot->m_pLeft);if (pRoot->m_pRight != NULL)PrintTree(pRoot->m_pRight);}}//销毁void DestroyTree(BinaryTreeNode* pRoot){if (pRoot != NULL){BinaryTreeNode* pLeft = pRoot->m_pLeft;BinaryTreeNode* pRight = pRoot->m_pRight;delete pRoot;pRoot = NULL;DestroyTree(pLeft);DestroyTree(pRight);}}//树的深度int TreeDepth(BinaryTreeNode* pRoot){if (pRoot == NULL)return 0;int nLeft = TreeDepth(pRoot->m_pLeft);int nRight = TreeDepth(pRoot->m_pRight);return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);}void FindPath(BinaryTreeNode* pRoot, int expectedSum, std::vector<int>& path, int& currentSum);//寻找路径void FindPath(BinaryTreeNode* pRoot, int expectedSum){if(pRoot == NULL)return;std::vector<int> path;int currentSum = 0;FindPath(pRoot, expectedSum, path, currentSum);}void FindPath(BinaryTreeNode*   pRoot,        int               expectedSum,  std::vector<int>& path,         int&              currentSum){currentSum += pRoot->m_nValue;path.push_back(pRoot->m_nValue);// 如果是叶结点,并且路径上结点的和等于输入的值// 打印出这条路径bool isLeaf = pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL;if(currentSum == expectedSum && isLeaf){cout<<"A path is found: ";std::vector<int>::iterator iter = path.begin();for(; iter != path.end(); ++ iter)cout<<*iter<<"\t";cout<<endl;}// 如果不是叶结点,则遍历它的子结点if(pRoot->m_pLeft != NULL)FindPath(pRoot->m_pLeft, expectedSum, path, currentSum);if(pRoot->m_pRight != NULL)FindPath(pRoot->m_pRight, expectedSum, path, currentSum);// 在返回到父结点之前,在路径上删除当前结点,// 并在currentSum中减去当前结点的值currentSum -= pRoot->m_nValue;path.pop_back();} int main(){BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);ConnectTreeNodes(pNode10, pNode5, pNode12);ConnectTreeNodes(pNode5, pNode4, pNode7);FindPath(pNode10,22);system("PAUSE");return 0;}
结果:




0 0
原创粉丝点击