a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C > 正文

如何在二叉樹中找出和為某一值的所有路徑

2020-01-26 16:03:31
字體:
供稿:網(wǎng)友
代碼如下所示,不足之處,還望指正!
復(fù)制代碼 代碼如下:

// BinaryTree.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//C++實(shí)現(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<<"請(qǐng)輸入根節(jié)點(diǎn):"<<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é)點(diǎn)開始往下訪問,一直到葉節(jié)點(diǎn)所經(jīng)過的所有節(jié)點(diǎn)形成一條路徑。輸出和與某個(gè)數(shù)相等的所有路徑。
例如: 二叉樹
         3
      2     6
   5    4
則和為9的,路徑有兩條,一條為3,6  另一條為3, 2, 4。 
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 色老头av | 亚洲国产一区二区三区 | 天天干天天操天天爽 | 国产精品久久久久久久久岛 | 日韩在线免费 | 亚洲第一视频 | 国产区在线观看视频 | 久久全国免费视频 | 禁果av一区二区三区 | av影片在线播放 | 欧美激情一区二区三区蜜桃视频 | 国产免费一区二区三区四区五区 | 日韩成人高清电影 | 久久亚洲一区 | 中文二区 | 亚洲久久| 国产国拍亚洲精品av | 国产高清精品一区二区三区 | 国产一区二区h | 青草免费 | 91精品综合久久久久久五月天 | 色综合视频 | 一区二区三区 在线 | 青青草网| 欧美在线a| 精品 99 | 欧洲av片| 91久久久精品视频 | 日韩免费视频一区二区 | 伊人av在线免费观看 | 北条麻妃国产九九九精品小说 | 日日操天天操 | 欧美成在线观看 | 欧美日韩精品一区 | 日日骑夜夜操 | 亚洲一级性 | 91精品久久久久久久久久久久久久久 | 日本1区2区 | 亚洲视频一区二区三区 | k8久久久一区二区三区 | a黄视频|