JZ-C-25

剑指offer第二十五题:二叉树中和为某一值的路径

  1 //============================================================================
  2 // Name        : JZ-C-25.cpp
  3 // Author      : Laughing_Lz
  4 // Version     :
  5 // Copyright   : All Right Reserved
  6 // Description : 二叉树中和为某一值的路径
  7 //============================================================================
  8 
  9 #include <iostream>
 10 #include <stdio.h>
 11 #include "BinaryTree.h"
 12 #include <vector>
 13 
 14 using namespace std;
 15 
 16 void FindPath(BinaryTreeNode* pRoot, int expectedSum, std::vector<int>& path,
 17         int& currentSum); //函数声明
 18 
 19 void FindPath(BinaryTreeNode* pRoot, int expectedSum) {
 20     if (pRoot == NULL) {
 21         return;
 22     }
 23     std::vector<int> path;
 24     int currentSum = 0; //当前路径值和
 25     FindPath(pRoot, expectedSum, path, currentSum);
 26 }
 27 void FindPath(BinaryTreeNode* pRoot, int expectedSum, std::vector<int>& path,
 28         int& currentSum) {
 29     currentSum += pRoot->m_nValue;
 30     path.push_back(pRoot->m_nValue);
 31     // 如果是叶结点,并且路径上结点的和等于输入的值
 32     // 打印出这条路径
 33     bool isLeaf = pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL;
 34     if (isLeaf && currentSum == expectedSum) {
 35         printf("A path is found: ");
 36         std::vector<int>::iterator it = path.begin();
 37         for (; it != path.end(); it++) {
 38             printf("%d\t", *it);
 39         }
 40         printf("\n");
 41     }
 42     // 如果不是叶结点,则遍历它的子结点
 43     if (pRoot->m_pLeft != NULL)
 44         FindPath(pRoot->m_pLeft, expectedSum, path, currentSum);
 45     if (pRoot->m_pRight != NULL)
 46         FindPath(pRoot->m_pRight, expectedSum, path, currentSum);
 47 
 48     // 在返回到父结点之前,在路径上删除当前结点,
 49     // 并在currentSum中减去当前结点的值 ★★
 50     currentSum -= pRoot->m_nValue;
 51     path.pop_back();
 52 
 53 }
 54 // ====================测试代码====================
 55 void Test(char* testName, BinaryTreeNode* pRoot, int expectedSum) {
 56     if (testName != NULL)
 57         printf("%s begins:\n", testName);
 58 
 59     FindPath(pRoot, expectedSum);
 60 
 61     printf("\n");
 62 }
 63 
 64 //            10
 65 //         /      \
 66 //        5        12
 67 //       /\
 68 //      4  7
 69 // 有两条路径上的结点和为22
 70 void Test1() {
 71     BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
 72     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
 73     BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
 74     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
 75     BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
 76 
 77     ConnectTreeNodes(pNode10, pNode5, pNode12);
 78     ConnectTreeNodes(pNode5, pNode4, pNode7);
 79 
 80     printf("Two paths should be found in Test1.\n");
 81     Test("Test1", pNode10, 22);
 82 
 83     DestroyTree(pNode10);
 84 }
 85 
 86 //            10
 87 //         /      \
 88 //        5        12
 89 //       /\
 90 //      4  7
 91 // 没有路径上的结点和为15
 92 void Test2() {
 93     BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
 94     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
 95     BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
 96     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
 97     BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
 98 
 99     ConnectTreeNodes(pNode10, pNode5, pNode12);
100     ConnectTreeNodes(pNode5, pNode4, pNode7);
101 
102     printf("No paths should be found in Test2.\n");
103     Test("Test2", pNode10, 15);
104 
105     DestroyTree(pNode10);
106 }
107 
108 //               5
109 //              /
110 //             4
111 //            /
112 //           3
113 //          /
114 //         2
115 //        /
116 //       1
117 // 有一条路径上面的结点和为15
118 void Test3() {
119     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
120     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
121     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
122     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
123     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
124 
125     ConnectTreeNodes(pNode5, pNode4, NULL);
126     ConnectTreeNodes(pNode4, pNode3, NULL);
127     ConnectTreeNodes(pNode3, pNode2, NULL);
128     ConnectTreeNodes(pNode2, pNode1, NULL);
129 
130     printf("One path should be found in Test3.\n");
131     Test("Test3", pNode5, 15);
132 
133     DestroyTree(pNode5);
134 }
135 
136 // 1
137 //  \
138 //   2
139 //    \
140 //     3
141 //      \
142 //       4
143 //        \
144 //         5
145 // 没有路径上面的结点和为16
146 void Test4() {
147     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
148     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
149     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
150     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
151     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
152 
153     ConnectTreeNodes(pNode1, NULL, pNode2);
154     ConnectTreeNodes(pNode2, NULL, pNode3);
155     ConnectTreeNodes(pNode3, NULL, pNode4);
156     ConnectTreeNodes(pNode4, NULL, pNode5);
157 
158     printf("No paths should be found in Test4.\n");
159     Test("Test4", pNode1, 16);
160 
161     DestroyTree(pNode1);
162 }
163 
164 // 树中只有1个结点
165 void Test5() {
166     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
167 
168     printf("One path should be found in Test5.\n");
169     Test("Test5", pNode1, 1);
170 
171     DestroyTree(pNode1);
172 }
173 
174 // 树中没有结点
175 void Test6() {
176     printf("No paths should be found in Test6.\n");
177     Test("Test6", NULL, 0);
178 }
179 
180 int main(int argc, char** argv) {
181     Test1();
182     Test2();
183     Test3();
184     Test4();
185     Test5();
186     Test6();
187 
188     return 0;
189 }

 

posted @ 2016-06-13 16:38  回看欧洲  阅读(274)  评论(1编辑  收藏  举报