生活随笔
收集整理的這篇文章主要介紹了
二叉树的操作3
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
【查找與定位算法】
問題描述:定位二叉樹 bt 中結(jié)點值為 x 的結(jié)點及其所在的層次。
題目挺簡單的,但是還是想了很久,說到底還是菜啊
代碼如下:
#include<bits/stdc++.h>
#define telemtype char
using namespace std;typedef struct node{telemtype data;struct node *lchild,*rchild;
} *Bitree; void creatree(Bitree &tt)
{char c;cin>>c;if(c=='#') tt=NULL;else{tt=(node *)malloc(sizeof(node));tt->data=c;creatree(tt->lchild);creatree(tt->rchild);}
}int check(char c,Bitree &t,int &num,int l)
{if(t){l++;if(t->data==c){num=l;}check(c,t->lchild,num,l);check(c,t->rchild,num,l);}
}int main()
{Bitree t;creatree(t);while(1){cout<<"請輸入需要查詢的節(jié)點:"<<endl;char c;cin>>c;int num;check(c,t,num,0);cout<<"結(jié)點所在層次為:"<<num<<endl;}
}
努力加油a啊,(o)/~
總結(jié)
以上是生活随笔為你收集整理的二叉树的操作3的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。