二叉树:路径之和 Path Sum
生活随笔
收集整理的這篇文章主要介紹了
二叉树:路径之和 Path Sum
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
給定一個二叉樹與整數(shù)sum,找出所有從根節(jié)點到葉結(jié)點的路徑,這些路 徑上的節(jié)點值累加和為sum
即創(chuàng)建一個二叉樹,要求二叉樹中有一個路徑從根節(jié)點到葉節(jié)點到路徑加起來代表到和為 給定的sum
如下二叉樹
給定路徑之和為18,則需要輸出兩條路徑:
[1,4,5,8]
[1,4,6,7]
同樣,這個過程我們可以使用先序深度優(yōu)先搜索,同時需要使用臨時數(shù)據(jù)結(jié)構(gòu)保存搜索過程中出現(xiàn)的根節(jié)點,方便回溯。
實現(xiàn)過程如下:
void getpath(Tree *root, int &path_value, int num, std::vector<int> &path, std::vector<std::vector<int> > &result) {if (root == NULL) {return;}/*搜索的過程中計算和*/path_value += root->data;path.push_back(root->data);/*當訪問到了葉子節(jié)點,且滿足和為sum時,將最終的路徑path加入到路徑列表*/if (root -> left == NULL && root ->left == NULL && path_value == num) {result.push_back(path);}else if (path_value > num) { //剪枝,當當前路徑之和已經(jīng)大于sum,后面肯定也不存在了,所以后序當前節(jié)點的子節(jié)點遍歷就沒有必要了path_value -= root -> data;path.pop_back();}/*搜索左孩子*/getpath(root->left, path_value, num, path, result);/*搜索右孩子*/getpath(root->right, path_value, num, path, result);path_value -= root ->data;path.pop_back();
}
/*初始化并獲取返回值*/
std::vector<std::vector<int>> pathSum(Tree *root, int num) {std::vector<std::vector<int>> result;std::vector<int> path;int path_value = 0;getpath(root,path_value, num, path ,result);return result;
}
測試代碼如下:
#include <iostream>
#include <stack>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>using namespace std;typedef struct tree
{int data;struct tree *left;struct tree *right;
}Tree,*TreeNode;/*創(chuàng)建二叉樹*/
void createTree(TreeNode *root) {char val = 0;cin >> val;if (val == '*') {(*root) = NULL;} else {(*root) = (Tree *)malloc(sizeof(tree));if ((*root) == NULL) {cout << "create node failed " << endl;exit(-1);} else {(*root)->data = val - '0';createTree(&(*root)->left);createTree(&(*root)->right);}}
}
/*獲取指定和為sum的路徑*/
void getpath(Tree *root, int &path_value, int num, std::vector<int> &path, std::vector<std::vector<int> > &result) {if (root == NULL) {return;}path_value += root->data;path.push_back(root->data);if (root -> left == NULL && root ->left == NULL && path_value == num) {result.push_back(path);} else if (path_value > num) {path_value -= root -> data;path.pop_back();}getpath(root->left, path_value, num, path, result);getpath(root->right, path_value, num, path, result);path_value -= root ->data;path.pop_back();
}
/*初始化路徑*/
std::vector<std::vector<int>> pathSum(Tree *root, int num) {std::vector<std::vector<int>> result;std::vector<int> path;int path_value = 0;getpath(root,path_value, num, path ,result);return result;
}int main(int argc, char const *argv[])
{/* code */TreeNode root;cout << "construct the tree " << endl;createTree(&root);cout << "input sum path " << endl;int num;cin >> num;cout << "sum path is " << endl;std::vector<std::vector<int> > result;result = pathSum(root, num);for (int i = 0;i < result.size(); ++i) {for (int j = 0;j < result[i].size(); ++j) {cout << "[" << result[i][j] << "] ";}cout << endl;}return 0;
}
輸出如下:
#輸入:構(gòu)造文章開頭的二叉樹
construct the tree
123***458***67***
input sum path
18#輸出路徑
sum path is
[1] [4] [5] [8]
[1] [4] [6] [7]
總結(jié)
以上是生活随笔為你收集整理的二叉树:路径之和 Path Sum的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《春雪》第七句是什么
- 下一篇: 二叉树:最近的公共祖先 Lowest C