欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

本文實(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í)例。相信本文所述算法對大家C程序算法設(shè)計(jì)的學(xué)習(xí)有一定的借鑒價(jià)值。

上一篇:C語言實(shí)現(xiàn)計(jì)算樹的深度的方法

欄    目:C語言

下一篇:C語言金幣陣列問題解決方法

本文標(biāo)題:C語言實(shí)現(xiàn)找出二叉樹中某個(gè)值的所有路徑的方法

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3362.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有