【C/C++练习题】二叉树中和为某一值的路径

举报
王建峰 发表于 2021/11/19 02:57:11 2021/11/19
【摘要】 《剑指Offer》面试题34:二叉树中和为某一值的路径   1 题目 输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径   2 分析 本题目的是找出所有符合条件的路径,因此要遍历二叉树,使用先序遍历可以从根节点开始。 需要...

《剑指Offer》面试题34:二叉树中和为某一值的路径

 


1 题目

输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径

 

2 分析

本题目的是找出所有符合条件的路径,因此要遍历二叉树,使用先序遍历可以从根节点开始。

需要一个辅助空间存储遍历过的树节点。当程序遍历到叶子节点的时候,如果满足所有路径和为指定值,打印路径。

在递归遍历节点过程中,当程序遍历完一条完整的路径,需要退回上一级父节点,此时也在路径中删除当前节点。

 

3 代码


  
  1. /*
  2. *********************************************************************************************************
  3. *
  4. * 模块名称 : 主程序
  5. * 文件名称 : demo.cpp
  6. * 版 本 : V1.0
  7. * 说 明 : 《剑指Offer》笔试题,二叉树和为某一值得路径
  8. *
  9. * 修改记录 :
  10. * 版本号 日期 作者 说明
  11. * V1.0 2019-08-13 hinzer 正式发布
  12. *
  13. * 码云链接:
  14. *
  15. *********************************************************************************************************
  16. */
  17. #include "iostream"
  18. #include "cstdlib"
  19. #include <vector>
  20. using namespace std;
  21. typedef struct Node
  22. {
  23. int m_nValue;
  24. Node* m_pLeft;
  25. Node* m_pRight;
  26. }BinaryTreeNode;
  27. void FindPath(BinaryTreeNode* pNode, int expectedSum);
  28. void FindPath(BinaryTreeNode* pNode, vector<int> &path , int expectedSum);
  29. /******************************************************************************
  30. * 函数介绍:确定二叉树和为某一值得路径
  31. * 输入参数:pNode二叉树头节点指针, expectedSum期望路径和
  32. * 输出参数:无
  33. * 返回值:无
  34. * 备注:无
  35. ******************************************************************************/
  36. void FindPath(BinaryTreeNode* pNode, int expectedSum)
  37. {
  38. if (NULL == pNode)
  39. return ;
  40. vector<int> path;
  41. FindPath(pNode, path, expectedSum);
  42. }
  43. /******************************************************************************
  44. * 函数介绍:确定二叉树和为某一值得路径
  45. * 输入参数:pNode二叉树头节点指针, path存储路径, expectedSum期望路径和
  46. * 输出参数:无
  47. * 返回值:无
  48. * 备注:无
  49. ******************************************************************************/
  50. void FindPath(BinaryTreeNode* pNode, vector<int> &path , int expectedSum)
  51. {
  52. path.push_back(pNode->m_nValue); //插入节点数值
  53. //1.回归条件--确定为叶子节点
  54. if ((NULL == pNode->m_pLeft) && (NULL == pNode->m_pRight))
  55. {
  56. if (pNode->m_nValue == expectedSum)
  57. {
  58. cout << "a path is found:";
  59. vector<int>::iterator iter = path.begin();
  60. for(;iter != path.end();++iter)
  61. {
  62. cout << *iter << "-";
  63. }
  64. cout << endl;
  65. }
  66. }
  67. //2.不是叶子节点,对子节点进行递归调用
  68. if (NULL != pNode->m_pLeft)
  69. FindPath(pNode->m_pLeft, path, expectedSum - pNode->m_nValue);
  70. if (NULL != pNode->m_pRight)
  71. FindPath(pNode->m_pRight, path, expectedSum - pNode->m_nValue);
  72. path.pop_back(); //退回父节点,在路径上要三处当前节点
  73. }
  74. /******************************************************************************
  75. * 函数介绍:功能测试函数
  76. * 输入参数:无
  77. * 输出参数:无
  78. * 返回值:无
  79. * 备注:无
  80. ******************************************************************************/
  81. void test01()
  82. {
  83. // 8(node1)
  84. // 6(node2) 10(node3)
  85. // 5(node4) 13(node5) 9(node6) 11(node7)
  86. BinaryTreeNode* node1 = new BinaryTreeNode();
  87. BinaryTreeNode* node2 = new BinaryTreeNode();
  88. BinaryTreeNode* node3 = new BinaryTreeNode();
  89. BinaryTreeNode* node4 = new BinaryTreeNode();
  90. BinaryTreeNode* node5 = new BinaryTreeNode();
  91. BinaryTreeNode* node6 = new BinaryTreeNode();
  92. BinaryTreeNode* node7 = new BinaryTreeNode();
  93. //node1
  94. node1->m_nValue = 8;
  95. node1->m_pLeft = node2;
  96. node1->m_pRight = node3;
  97. //node2
  98. node2->m_nValue = 6;
  99. node2->m_pLeft = node4;
  100. node2->m_pRight = node5;
  101. //node3
  102. node3->m_nValue = 10;
  103. node3->m_pLeft = node6;
  104. node3->m_pRight = node7;
  105. //node4
  106. node4->m_nValue = 5;
  107. node4->m_pLeft = nullptr;
  108. node4->m_pRight = nullptr;
  109. //node5
  110. node5->m_nValue = 13;
  111. node5->m_pLeft = nullptr;
  112. node5->m_pRight = nullptr;
  113. //node6
  114. node6->m_nValue = 9;
  115. node6->m_pLeft = nullptr;
  116. node6->m_pRight = nullptr;
  117. //node7
  118. node7->m_nValue = 11;
  119. node7->m_pLeft = nullptr;
  120. node7->m_pRight = nullptr;
  121. //测试部分
  122. FindPath(node1, 27);
  123. delete node1;delete node2;
  124. delete node3;delete node4;
  125. delete node5;delete node6;
  126. delete node7;
  127. }
  128. int main(int argc, char const *argv[])
  129. {
  130. test01();
  131. return 0;
  132. }

 

4 运行结果

文章来源: blog.csdn.net,作者:hinzer,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/feit2417/article/details/99544525

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。