剑指Offer----面试题25:二叉树中和为某一值的路径
来源:互联网 发布:兄弟连it教育地址 编辑:程序博客网 时间:2024/05/22 16:51
题目:
题目:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根节点往下一直到叶节点所经过的结点形成一条路径。
分析:
官方源代码:
/*题目:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根节点往下一直到叶节点所经过的结点形成一条路径。*/#include"BinaryTree.h"#include<vector>#include<iostream>using std::cout;using std::endl;using std::vector;using namespace OrdinaryBinaryTreeSpace5;void FindPath1(BinaryTreeNode *root, int exceptedSum);void FindPath1(BinaryTreeNode *root, int exceptedSum, vector<int> &vec, int currentSum);void FindPath1(BinaryTreeNode *root, int exceptedSum){//如果树为空,提示树为空if (root == NULL){cout << "The tree is empty" << endl;return;}vector<int> vec;int currentSum = 0;FindPath1(root, exceptedSum, vec, currentSum);}void FindPath1(BinaryTreeNode *root, int exceptedSum, vector<int> &vec, int currentSum){//将该结点压入容器,因为是递归,所以用容器模拟的栈,用容器的原因是为了便于打印元素vec.push_back(root->element);currentSum += root->element;//如果是叶子结点且当前值等于期望的值,则打印输出各个结点if (currentSum == exceptedSum && root->left == NULL && root->right == NULL){cout << "A path is found:";for (auto it = vec.begin(); it != vec.end(); it++)cout << *it << " ";cout << endl;}//如果左子树为不为空,递归调用左子树if (root->left != NULL)FindPath1(root->left, exceptedSum, vec, currentSum);//如果右子树为不为空,递归调用右子树if (root->right != NULL)FindPath1(root->right, exceptedSum, vec, currentSum);//再返回父节点之后,在路径上删除当前结点vec.pop_back();}void test11(){cout << "\t============二叉树中和为某一值的路径(存在)=============" << endl;BinaryTreeNode *node1 = CreateBinaryTreeNode(10);BinaryTreeNode *node2 = CreateBinaryTreeNode(5);BinaryTreeNode *node3 = CreateBinaryTreeNode(12);BinaryTreeNode *node4 = CreateBinaryTreeNode(4);BinaryTreeNode *node5 = CreateBinaryTreeNode(7);ConnectBinaryTreeNodes(node1, node2, node3);ConnectBinaryTreeNodes(node2, node4, node5);ConnectBinaryTreeNodes(node3, NULL, NULL);ConnectBinaryTreeNodes(node4, NULL, NULL);ConnectBinaryTreeNodes(node5, NULL, NULL);FindPath1(node1, 22);DestoryTree(node1);}void test12(){cout << "\t============二叉树中和为某一值的路径(bu存在)=============" << endl;BinaryTreeNode *node1 = CreateBinaryTreeNode(10);BinaryTreeNode *node2 = CreateBinaryTreeNode(5);BinaryTreeNode *node3 = CreateBinaryTreeNode(12);BinaryTreeNode *node4 = CreateBinaryTreeNode(4);BinaryTreeNode *node5 = CreateBinaryTreeNode(7);ConnectBinaryTreeNodes(node1, node2, node3);ConnectBinaryTreeNodes(node2, node4, node5);ConnectBinaryTreeNodes(node3, NULL, NULL);ConnectBinaryTreeNodes(node4, NULL, NULL);ConnectBinaryTreeNodes(node5, NULL, NULL);FindPath1(node1, 9);DestoryTree(node1);}void test13(){cout << "\t============二叉树中和为某一值的路径(树为空)=============" << endl;FindPath1(NULL, 9);}int main1(){test11();cout << endl;test12();cout << endl;test13();cout << endl;system("pause");return 0;}
运行结果:
Two paths should be found in Test1.Test1 begins:A path is found: 10 5 7A path is found: 10 12No paths should be found in Test2.Test2 begins:One path should be found in Test3.Test3 begins:A path is found: 5 4 3 2 1No paths should be found in Test4.Test4 begins:One path should be found in Test5.Test5 begins:A path is found: 1No paths should be found in Test6.Test6 begins:请按任意键继续. . .
0 0
- 【剑指offer】面试题25:二叉树中和为某一值的路径
- 剑指Offer:面试题25 二叉树中和为某一值的路径
- 剑指offer 面试题25 求二叉树中和为某一路径的值
- 《剑指Offer》面试题25:二叉树中和为某一值的路径
- 剑指offer 面试题25—二叉树中和为某一值的路径
- 剑指offer--面试题25:二叉树中和为某一值的路径--Java实现
- 【剑指Offer学习】【面试题25:二叉树中和为某一值的路径】
- 剑指Offer面试题25(Java版):二叉树中和为某一值的路径
- 剑指offer面试题25-二叉树中和为某一值的路径
- 剑指offer-面试题25:二叉树中和为某一值的路径
- 剑指offer之面试题25:二叉树中和为某一值的路径
- 剑指Offer----面试题25:二叉树中和为某一值的路径
- 剑指offer-面试题25:二叉树中和为某一值的路径
- 剑指offer面试题25:二叉树中和为某一值的路径
- 【剑指offer】面试题25:二叉树中和为某一值的路径
- 剑指offer--面试题25: 二叉树中和为某一值的路径
- 剑指Offer面试题25(Java版):二叉树中和为某一值的路径
- 剑指offer——面试题25:二叉树中和为某一值的路径
- mybatis关系映射之一对多和多对一
- _00017 Kafka的体系结构介绍以及Kafka入门案例(初级案例+Java API的使用)
- 前端异步加载js且判断js是不是加载完成
- OpenWrt编程篇
- 核心流程 - 【Dr.Elephant源码分析系列文章-2】
- 剑指Offer----面试题25:二叉树中和为某一值的路径
- Java的GC的内部原理
- 数据结构
- MYSQL中IFNULL的使用 以及统计2张表的字段和
- LaTeX新手上路
- angularjs编写一颗3级目录的树结构
- 大型网站架构系列:缓存在分布式系统中的应用(二)
- easyUI datagrid可编辑行JS代码片段总结
- 移动计算概念