欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年09月16日 16:25:48   投稿:shichen2014  
這篇文章主要介紹了C語言實(shí)現(xiàn)找出二叉樹中某個(gè)值的所有路徑的方法,針對(duì)數(shù)據(jù)結(jié)構(gòu)中二叉樹的實(shí)用操作技巧,需要的朋友可以參考下

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

具體實(shí)現(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);
}

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

相關(guān)文章

最新評(píng)論