国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言實現(xiàn)找出二叉樹中某個值的所有路徑的方法

2020-01-26 15:18:47
字體:
供稿:網(wǎng)友

本文實例講述了C語言實現(xiàn)找出二叉樹中某個值的所有路徑的方法,是非常常用的一個實用算法技巧。分享給大家供大家參考。

具體實現(xiàn)方法如下:

#include <iostream>#include <vector>#include <iterator>#include <algorithm>using namespace std;vector<int> result;struct Node { Node(int i = 0, Node *pl = NULL, Node *pr = NULL) : data(i), left(pl), right(pr) {} int data; Node *left; Node *right;};Node* Construct(){ Node *node4 = new Node(7); Node *node3 = new Node(4); Node *node2 = new Node(12); Node *node1 = new Node(5, node3, node4); Node *root = new Node(10, node1, node2); return root;}void print(){ copy(result.begin(), result.end(), ostream_iterator<int>(cout, " ")); cout << endl;}void PrintSum(Node *root, int sum){ if(root == NULL) return; result.push_back(root->data); if(root->left == NULL && root->right == NULL && root->data == sum) { print(); } PrintSum(root->left, sum - root->data); PrintSum(root->right, sum - root->data); result.pop_back();}void main(){ Node *root = Construct(); PrintSum(root, 22);}

感興趣的朋友可以測試運行一下本文實例。相信本文所述算法對大家C程序算法設(shè)計的學(xué)習(xí)有一定的借鑒價值。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 凤山县| 泰州市| 鹿邑县| 那曲县| 承德市| 东兴市| 新郑市| 上虞市| 郸城县| 仙游县| 于都县| 司法| 武威市| 开远市| 庐江县| 霞浦县| 白城市| 兴隆县| 博白县| 寿宁县| 贵港市| 泾阳县| 常德市| 光山县| 萨嘎县| 泰顺县| 德庆县| 宜良县| 青海省| 资溪县| 讷河市| 景洪市| 宿州市| 承德市| 永寿县| 左云县| 双牌县| 马公市| 洪洞县| 衢州市| 泗水县|