建立二叉树A【openjudge】
生活随笔
收集整理的這篇文章主要介紹了
建立二叉树A【openjudge】
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
總時(shí)間限制:?1000ms?內(nèi)存限制:65535kB 輸入第一行輸入一個(gè)整數(shù)t,代表測(cè)試數(shù)據(jù)的組數(shù)。
對(duì)于每組測(cè)試數(shù)據(jù),第一行輸入一個(gè)整數(shù)n,代表二叉樹(shù)根節(jié)點(diǎn)到各葉子節(jié)點(diǎn)的路徑數(shù)目。
隨后輸入n行,每行包含一個(gè)字符串S, 代表從根節(jié)點(diǎn)走向?qū)?yīng)葉子節(jié)點(diǎn)的路徑。
路徑中每個(gè)節(jié)點(diǎn)用兩個(gè)字符表達(dá):第一個(gè)字符要么為 ‘+’ 要么為 ‘-‘, ‘+’代表網(wǎng)左孩子方向走,’-’代表往右孩子方向走。第二個(gè)字符是一個(gè)大寫的英文字母,表示對(duì)應(yīng)節(jié)點(diǎn)編號(hào)。
比如 +B-C 代表 從根結(jié)點(diǎn)向左走到B節(jié)點(diǎn),再向右走到C節(jié)點(diǎn)。
根節(jié)點(diǎn)編號(hào)始終是字符‘A’,不會(huì)有重復(fù)編號(hào)的節(jié)點(diǎn)。
字符串S長(zhǎng)度不超過(guò)20 , n <= 26 輸出每組測(cè)試數(shù)據(jù),輸出一行,對(duì)應(yīng)二叉樹(shù)的中序遍歷結(jié)果。 樣例輸入 3 3 +B-D -C+E -C-F 2 +B -C 1 +B+C+D+E 樣例輸出 BDAECF BAC EDCBA 提示二叉樹(shù)的節(jié)點(diǎn)用結(jié)構(gòu)體存儲(chǔ)。
結(jié)構(gòu)體聲明為:
struct Node
{
char c;
struct Node *ls , *rs;
};
創(chuàng)建一個(gè)新節(jié)點(diǎn)通過(guò)malloc函數(shù)實(shí)現(xiàn)
Node *node = (Node *)malloc(sizeof(Node));
如果對(duì)于指針使用不熟悉,也可以采用數(shù)組下標(biāo)來(lái)實(shí)現(xiàn)。
結(jié)構(gòu)體聲明成以下形式:
struct Node
{
char c;
int ls , rs;
};
同時(shí)開(kāi)辟一個(gè)數(shù)組
struct Node node[110];
給出一顆二叉樹(shù)根節(jié)點(diǎn)到各葉子節(jié)點(diǎn)的路徑,建立這顆二叉樹(shù),輸出中序遍歷的結(jié)果。
對(duì)于每組測(cè)試數(shù)據(jù),第一行輸入一個(gè)整數(shù)n,代表二叉樹(shù)根節(jié)點(diǎn)到各葉子節(jié)點(diǎn)的路徑數(shù)目。
隨后輸入n行,每行包含一個(gè)字符串S, 代表從根節(jié)點(diǎn)走向?qū)?yīng)葉子節(jié)點(diǎn)的路徑。
路徑中每個(gè)節(jié)點(diǎn)用兩個(gè)字符表達(dá):第一個(gè)字符要么為 ‘+’ 要么為 ‘-‘, ‘+’代表網(wǎng)左孩子方向走,’-’代表往右孩子方向走。第二個(gè)字符是一個(gè)大寫的英文字母,表示對(duì)應(yīng)節(jié)點(diǎn)編號(hào)。
比如 +B-C 代表 從根結(jié)點(diǎn)向左走到B節(jié)點(diǎn),再向右走到C節(jié)點(diǎn)。
根節(jié)點(diǎn)編號(hào)始終是字符‘A’,不會(huì)有重復(fù)編號(hào)的節(jié)點(diǎn)。
字符串S長(zhǎng)度不超過(guò)20 , n <= 26
結(jié)構(gòu)體聲明為:
struct Node
{
char c;
struct Node *ls , *rs;
};
創(chuàng)建一個(gè)新節(jié)點(diǎn)通過(guò)malloc函數(shù)實(shí)現(xiàn)
Node *node = (Node *)malloc(sizeof(Node));
如果對(duì)于指針使用不熟悉,也可以采用數(shù)組下標(biāo)來(lái)實(shí)現(xiàn)。
結(jié)構(gòu)體聲明成以下形式:
struct Node
{
char c;
int ls , rs;
};
同時(shí)開(kāi)辟一個(gè)數(shù)組
struct Node node[110];
用一個(gè)變量p來(lái)控制每次新節(jié)點(diǎn)的申請(qǐng)。
#include <stdio.h> #include <malloc.h>#define MAXSIZE 100 char str[MAXSIZE]; typedef struct node {char data;struct node *lchild;struct node *rchild; }Btree; Btree *b;void creatree(char *str) {char *ch=str;Btree *t=b,*s;while(*ch!='\0'){switch(*ch){case '+':ch++;s=(Btree *)malloc(sizeof(Btree));s->lchild=NULL;s->rchild=NULL;s->data=*ch;if(t->lchild==NULL)t->lchild=s;t=t->lchild;break;case '-':ch++;s=(Btree *)malloc(sizeof(Btree));s->lchild=NULL;s->rchild=NULL;s->data=*ch;if(t->rchild==NULL)t->rchild=s;t=t->rchild;break;}ch++;} } void inorder (Btree *root) {if(root!=NULL){inorder (root->lchild);printf("%c",root->data);inorder(root->rchild);} } int main() {int t,n;scanf("%d",&t);while(t--){b=(Btree *)malloc(sizeof(Btree));b->lchild=NULL;b->rchild=NULL;b->data='A';scanf("%d",&n);getchar();while(n--){gets(str);creatree(str);}inorder(b);printf("\n");}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/unclejelly/archive/2013/05/12/4082165.html
總結(jié)
以上是生活随笔為你收集整理的建立二叉树A【openjudge】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 谈javascript变量声明
- 下一篇: Unsupported compiler