剑指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
原创粉丝点击