C++實現(xiàn)查找二叉樹中和為某一值的所有路徑的示例
從樹的根結(jié)點開始往下訪問一直到葉結(jié)點所經(jīng)過的所有結(jié)點形成一條路徑。
打印出和與輸入整數(shù)相等的所有路徑。
例如 輸入整數(shù)22和如下二元樹
則打印出兩條路徑:10, 12和10, 5, 7。
先序遍歷樹即可得到結(jié)果。
算法: FindPath(BTree * root,int sum,int target,Stack * s) 用來計算,sum為棧中的元素的和,target為目標(biāo)值。
到達一個節(jié)點之后計算當(dāng)前節(jié)點和sum的和,如果為target,輸出路徑返回,如果大于target,則直接返回,如果小于,則將當(dāng)前節(jié)點的值入棧,更新sum的值,繼續(xù)遍歷,遍歷完成之后,也就是從當(dāng)前節(jié)點返回的時候,將其從棧中彈出,更新sum
代碼如下(GCC編譯通過):
#include "stdio.h" #include "stdlib.h" #define MAXSIZE 8 typedef struct node { int data; struct node * left; struct node * right; }BTree; typedef struct { int top; int data[MAXSIZE]; }Stack; BTree * CreatTree(int a[],int n); void Iorder(BTree * root); void Porder(BTree * root); void FindPath(BTree * root,int sum,int target,Stack * stack); void InitStack(Stack * stack); void Push(Stack * s,int val); int Pop(Stack *s); int main(void) { int array[MAXSIZE] = {5,3,8,7,2,4,1,9},target; BTree * root; Stack stack; target = 12; root = CreatTree(array,MAXSIZE); InitStack(&stack); printf("二叉樹內(nèi)元素升序排列:"); Iorder(root); printf("\n"); printf("目標(biāo)值:%d,路徑:",target); FindPath(root,0,target,&stack); printf("\n"); return 0; } //根據(jù)數(shù)組生成二叉排序樹 BTree * CreatTree(int a[],int n) { BTree * root ,*p,*cu,*pa; int i; root = (BTree *)malloc(sizeof(BTree)); root->data = a[0]; root->left = root->right =NULL; for(i=1;i<n;i++) { p = (BTree *)malloc(sizeof(BTree)); p->data = a[i]; p->left = p->right =NULL; cu = root; while(cu) { pa = cu; if(cu->data > p->data) cu = cu->left; else cu = cu->right; } if(pa->data > p->data) pa->left = p; else pa->right = p; } return root; } //中根遍歷,打印二叉樹 void Iorder(BTree * root) { if(root) { Iorder(root->left); printf("%3d",root->data); Iorder(root->right); } } //尋找路徑 void FindPath(BTree * root,int sum,int target,Stack * s) { int i; if(!root) return ; if(sum + root->data == target) { Push(s,root->data); for(i = 0;i<s->top;i++) printf("%3d",s->data[i]); return; } else if(sum + root->data > target) { return; } else { Push(s,root->data); sum += root->data; FindPath(root->left,sum,target,s); FindPath(root->right,sum,target,s); sum -= root->data; Pop(s); } } //初始化棧 void InitStack(Stack * s) { s->top = 0; } //入棧 void Push(Stack *s,int val) { if(s->top == MAXSIZE) { printf("棧滿,無法入棧!\n"); return; } s->data[(s->top)++] = val; } //出棧 int Pop(Stack *s) { if(s->top == 0) { printf("???,無法出棧!\n"); return; } return s->data[--(s->top)]; }
上一篇:深入解析C++程序中激發(fā)事件和COM中的事件處理
欄 目:C語言
下一篇:詳解C++編程中斷言static_assert的使用
本文標(biāo)題:C++實現(xiàn)查找二叉樹中和為某一值的所有路徑的示例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2510.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(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語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 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ù)求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什