二叉树中和为某一值的路径

来源:互联网 发布:视频播放cms 编辑:程序博客网 时间:2024/05/21 02:52

题目描述

输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
#include <vector>#include <iostream>#include "BinaryTree.h"using namespace std;void FindPath(BinaryTreeNode* pRoot, int expectedNum, vector<BinaryTreeNode*> path, int currentNum){path.push_back(pRoot);currentNum += pRoot ->m_nValue;bool isLeave = pRoot ->m_pLeft == NULL && pRoot ->m_pRight == NULL;if(currentNum == expectedNum && isLeave){int size = path.size();for(int i = 0; i < size ;i++){printf("%d\t", path[i] ->m_nValue);}printf("\n");}if(pRoot ->m_pLeft != NULL)FindPath(pRoot->m_pLeft, expectedNum, path, currentNum);if(pRoot ->m_pRight != NULL)FindPath(pRoot->m_pRight, expectedNum, path, currentNum);currentNum -= pRoot ->m_nValue;path.pop_back();} void FindPath(BinaryTreeNode* pRoot ,int expectedNum) {if(pRoot == NULL)return;vector<BinaryTreeNode*> path;int currentNum = 0;FindPath (pRoot, expectedNum, path, currentNum); }

// ====================测试代码====================void Test(char* testName, BinaryTreeNode* pRoot, int expectedSum){    if(testName != NULL)        printf("%s begins:\n", testName);    FindPath(pRoot, expectedSum);    printf("\n");}//            10//         /      \//        5        12//       /\        //      4  7     // 有两条路径上的结点和为22void Test1(){    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);    printf("Two paths should be found in Test1.\n");    Test("Test1", pNode10, 22);    DestroyTree(pNode10);}//            10//         /      \//        5        12//       /\        //      4  7     // 没有路径上的结点和为15void Test2(){    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);    printf("No paths should be found in Test2.\n");    Test("Test2", pNode10, 15);    DestroyTree(pNode10);}//               5//              ///             4//            ///           3//          ///         2//        ///       1// 有一条路径上面的结点和为15void Test3(){    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);    ConnectTreeNodes(pNode5, pNode4, NULL);    ConnectTreeNodes(pNode4, pNode3, NULL);    ConnectTreeNodes(pNode3, pNode2, NULL);    ConnectTreeNodes(pNode2, pNode1, NULL);    printf("One path should be found in Test3.\n");    Test("Test3", pNode5, 15);    DestroyTree(pNode5);}// 1//  \//   2//    \//     3//      \//       4//        \//         5// 没有路径上面的结点和为16void Test4(){    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);    ConnectTreeNodes(pNode1, NULL, pNode2);    ConnectTreeNodes(pNode2, NULL, pNode3);    ConnectTreeNodes(pNode3, NULL, pNode4);    ConnectTreeNodes(pNode4, NULL, pNode5);    printf("No paths should be found in Test4.\n");    Test("Test4", pNode1, 16);    DestroyTree(pNode1);}// 树中只有1个结点void Test5(){    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);    printf("One path should be found in Test5.\n");    Test("Test5", pNode1, 1);    DestroyTree(pNode1);}// 树中没有结点void Test6(){    printf("No paths should be found in Test6.\n");    Test("Test6", NULL, 0);}int main(int argc, char* argv[]){    Test1();    Test2();    Test3();    Test4();    Test5();    Test6();    return 0;}

0 0
原创粉丝点击