面试100题:4.在二元树中找出和为某一值的所有路径

题目

输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。例如输入整数22和如下二元树

        10

       /   \

     5    12

   /   \

 4     7

则打印出两条路径:10, 12 和10, 5,7。

分析

当访问到某一结点时,把该结点添加到路径上,并累加当前结点的值。如果当前结点为叶结点并且当前路径的和刚好等于输入的整数,则当前的路径符合要求,我们把它打印出来。如果当前结点不是叶结点,则继续访问它的子结点。当前结点访问结束后,递归函数将自动回到父结点。因此我们在函数退出之前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是根结点到父结点的路径。我们不难看出保存路径的数据结构实际上是一个栈结构,因为路径要与递归调用状态一致,而递归调用本质就是一个压栈和出栈的过程。

  1. /*Title: 4.在二元树中找出和为某一值的所有路径 
  2. Author:  gocode 
  3. Date:    2012-10-08*/  
  4.   
  5. #include <iostream>  
  6. #include <vector>  
  7. using namespace std;  
  8.   
  9. // 定义二叉查找树节点  
  10. struct  BSTreeNode  
  11. {  
  12.     int m_nValue;  
  13.     BSTreeNode *m_pLeft;  
  14.     BSTreeNode *m_pRight;  
  15. };  
  16.   
  17. // 查找符合设定和的路径  
  18. // 此处借助vector数组进行压栈、出栈的操作,很方便  
  19. void FindPath(BSTreeNode *pTreeNode, int expectedSum, vector<int>& path, int& currentSum)  
  20. {  
  21.     if(!pTreeNode)  
  22.         return;  
  23.     currentSum += pTreeNode->m_nValue;  
  24.     path.push_back(pTreeNode->m_nValue);  
  25.    
  26.     // 如节点是叶子节点,并且相加之和与给定和相同,则打印之  
  27.     bool isLeaf = (!pTreeNode->m_pLeft && !pTreeNode->m_pRight);  
  28.     if(currentSum == expectedSum && isLeaf)  
  29.     {  
  30.         for(vector<int>::iterator iter = path.begin(); iter != path.end(); ++ iter)  
  31.             cout << *iter << " ";  
  32.         cout << endl;  
  33.     }  
  34.   
  35.     // 如果节点不是叶子节点,则继续递归调用左、右子树  
  36.     if(pTreeNode->m_pLeft)  
  37.         FindPath(pTreeNode->m_pLeft, expectedSum, path, currentSum);  
  38.     if(pTreeNode->m_pRight)  
  39.         FindPath(pTreeNode->m_pRight, expectedSum, path, currentSum);  
  40.    
  41.     // 当访问完一个节点后,返回到其父节点时,从当前和减去,并出栈  
  42.     currentSum -= pTreeNode->m_nValue;  
  43.     path.pop_back();  
  44. }  
  45.   
  46. // 添加节点  
  47. // 注意第一个参数,必须加&符号,否则指针不会被改变  
  48. void AddBSTreeNode(BSTreeNode *&pCurrent, int value)  
  49. {  
  50.     if(NULL == pCurrent)  
  51.     {  
  52.         BSTreeNode *pNewNode = new BSTreeNode();  
  53.         pNewNode->m_pLeft = NULL;  
  54.         pNewNode->m_pRight = NULL;  
  55.         pNewNode->m_nValue = value;  
  56.         pCurrent = pNewNode;  
  57.     }  
  58.     else  
  59.     {  
  60.         if(value < pCurrent->m_nValue)  
  61.             AddBSTreeNode(pCurrent->m_pLeft, value);  
  62.         else if(value > pCurrent->m_nValue)  
  63.             AddBSTreeNode(pCurrent->m_pRight, value);  
  64.         else  
  65.             cout<<"Duplicate value is not allowed."<<endl;  
  66.     }  
  67. }  
  68.   
  69. void main()  
  70. {  
  71.     vector<int> resultpath;  
  72.     int resultSum = 0;  
  73.     BSTreeNode * pRoot = NULL;  
  74.    
  75.     AddBSTreeNode(pRoot, 10);  
  76.     AddBSTreeNode(pRoot, 5);  
  77.     AddBSTreeNode(pRoot, 12);  
  78.     AddBSTreeNode(pRoot, 4);  
  79.     AddBSTreeNode(pRoot, 7);  
  80.    
  81.     cout<<"Result is: "<<endl;  
  82.     FindPath(pRoot, 22, resultpath, resultSum);  
  83.     system("paus
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值