wiki 3143 二叉樹的前序、中序及後序遍曆

來源:互聯網
上載者:User

標籤:style   blog   io   os   for   sp   on   2014   log   

先序遍曆:訪問根,遍曆左子樹,遍曆右子樹,簡稱:DLR。

中序遍曆:遍曆左子樹,訪問根,遍曆右子樹,簡稱:LDR。

後序遍曆:遍曆左子樹,遍曆右子樹,訪問根,簡稱:LRD。

數組搞的:

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<cmath>#include<bitset>#define mem(a,b) memset(a,b,sizeof(a))#define lson i<<1,l,mid#define rson i<<1|1,mid+1,r#define llson j<<1,l,mid#define rrson j<<1|1,mid+1,r#define INF 1000000007#define seed 13131#define seed1 1313#define maxn 500005typedef long long ll;typedef unsigned long long ull;using namespace std;int n,ch[18][2];void pre(int i){    cout<<i<<' ';    if(ch[i][0]) pre(ch[i][0]);    if(ch[i][1]) pre(ch[i][1]);}void mid(int i){    if(ch[i][0]) mid(ch[i][0]);    cout<<i<<' ';    if(ch[i][1]) mid(ch[i][1]);}void beh(int i){    if(ch[i][0]) beh(ch[i][0]);    if(ch[i][1]) beh(ch[i][1]);    cout<<i<<' ';}int main(){    scanf("%d",&n);    int l,r;    for(int i=1;i<=n;i++)    {        scanf("%d%d",&l,&r);        ch[i][0]=l,ch[i][1]=r;    }    pre(1);    cout<<endl;    mid(1);    cout<<endl;    beh(1);    cout<<endl;    return 0;}


wiki 3143 二叉樹的前序、中序及後序遍曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.