如何在二叉樹中找出和為某一值的所有路徑
代碼如下所示,不足之處,還望指正!
// BinaryTree.cpp : 定義控制臺應(yīng)用程序的入口點。
//C++實現(xiàn)鏈?zhǔn)蕉鏄洌诙鏄渲姓页龊蜑槟骋恢档乃新窂?BR>#include "stdafx.h"
#include<iostream>
#include<string>
#include <stack>
using namespace std;
static int sum(0);
static int count(0);
template<class T>
struct BiNode
{
T data;
struct BiNode<T> *rchild,*lchild;
};
template<class T>
class BiTree
{
public:
BiTree(){
cout<<"請輸入根節(jié)點:"<<endl;
Create(root);
if (NULL != root)
{
cout<<"root="<<root->data<<endl;
}
else
{
cout << "The BinaryTree is empty." << endl;
}
}
~BiTree(){Release(root);}
int Depth(){return Depth(root);}
int FindPath(T i)
{
stack<BiNode<T>*> sta;
return FindPath(i, root, sta);
};
private:
BiNode<T> *root;
void Create(BiNode<T>* &bt);
void Release(BiNode<T> *bt);
int Depth(BiNode<T>* bt);
int FindPath(T i, BiNode<T>* bt, stack<BiNode<T>*> &sta);
};
//析構(gòu)函數(shù)
template <class T>
void BiTree<T>::Release(BiNode<T> *bt)
{
if(bt==NULL)
{
Release(bt->lchild );
Release(bt->rchild );
delete bt;
}
}
//建立二叉樹
template <class T>
void BiTree<T>::Create(BiNode<T>* &bt)
{
T ch;
cin>>ch;
if(ch== 0)bt=NULL;
else
{
bt=new BiNode<T>;
bt->data =ch;
cout<<"調(diào)用左孩子"<<endl;
Create(bt->lchild );
cout<<"調(diào)用右孩子"<<endl;
Create(bt->rchild );
}
}
//求樹的深度
template <class T>
int BiTree<T>::Depth(BiNode<T>* bt)
{
if (NULL == bt)
{
return 0;
}
int d1 = Depth(bt->lchild);
int d2 = Depth(bt->rchild);
return (d1 > d2 ? d1 : d2)+ 1;
}
template <class T>
int BiTree<T>::FindPath(T i, BiNode<T>* bt, stack<BiNode<T>*> &sta)
{
if (NULL != bt)
{
sta.push(bt);
}
sum += bt->data;
if (sum == i && bt->lchild == NULL && bt->rchild == NULL)
{
stack<BiNode<T>*> sta2(sta);
BiNode<T>* p;
cout << "One of the path is: " ;
while (!sta2.empty())
{
p = sta2.top();
cout << p->data << " ";
sta2.pop();
}
cout << endl;
count ++;
}
if (NULL != bt->lchild)
{
FindPath(i, bt->lchild, sta);
}
if (NULL != bt->rchild)
{
FindPath(i,bt->rchild, sta);
}
sum -= bt->data;
sta.pop();
return count;
}
void main()
{
BiTree<int> a;
cout << "There are " << a.FindPath(9) << " path all." << endl;
}
輸入一棵二叉樹,從樹的根節(jié)點開始往下訪問,一直到葉節(jié)點所經(jīng)過的所有節(jié)點形成一條路徑。輸出和與某個數(shù)相等的所有路徑。
例如: 二叉樹
3
2 6
5 4
則和為9的,路徑有兩條,一條為3,6 另一條為3, 2, 4。
上一篇:libxml教程(圖文詳解)
欄 目:C語言
下一篇:用C++實現(xiàn)單向循環(huán)鏈表的解決方法
本文標(biāo)題:如何在二叉樹中找出和為某一值的所有路徑
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4386.html
您可能感興趣的文章
- 01-10深入二叉樹兩個結(jié)點的最低共同父結(jié)點的詳解
- 01-10如何判斷一個數(shù)是否為2的冪次方?若是,并判斷出來是多少次方
- 01-10如何判斷一個數(shù)是否為4的冪次方?若是,并判斷出來是多少次方
- 01-10如何查看進(jìn)程實際的內(nèi)存占用情況詳解
- 01-10深入解析最長公共子串
- 01-10如何尋找數(shù)組中的第二大數(shù)
- 01-10深入理解二叉樹的非遞歸遍歷
- 01-10大數(shù)(高精度數(shù))模板(分享)
- 01-10深入遍歷二叉樹的各種操作詳解(非遞歸遍歷)
- 01-10Linux C 獲取進(jìn)程退出值的實現(xiàn)代碼


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法