uva 699 The Falling Leaves(建二叉樹同時求和),uva二叉樹

來源:互聯網
上載者:User

uva 699 The Falling Leaves(建二叉樹同時求和),uva二叉樹

本來看著挺難的,大概是因為我多瞟了一眼題解,瞬間覺得簡單多了,做題就得這樣,多自己想想,現在是

多校聯賽,然而我並不會做。。。。慢慢來,一直在努力。

分析:

 題上說了做多不會超過80行,所以可以開一個數組,這裡我是把根節點作為第42個數,可以在建樹的同時求

出那一列所有數值的和左孩子節點減一,右孩子節點加一。。。寫的時候中間出了點小bug,忘了給flag重設0了,調

了好久。。。第一次提交wa了,因為沒有換行,題目要求結果之間有一行空行的。。。

貼代碼:

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<limits.h>int cnt[100];int flag,m;int min,max;typedef struct Tnode{int data;struct Tnode *lchild;struct Tnode *rchild;}*node;void creattree(node &T,int n){int x; if(flag){ x = m;  flag=0;    }elsescanf("%d",&x);if(x == -1){T = NULL;return ;}else{cnt[n] += x;T = (node)malloc(sizeof(Tnode));T->lchild = NULL;T->rchild = NULL;T->data = x;}creattree(T->lchild, n-1);creattree(T->rchild, n+1);if(n < min)min = n;if(n > max)max = n;return ;}int main(){int i;int ans = 0;while(scanf("%d",&m)&&m!=-1){ans++;min = INT_MAX;max = 0;flag = 1;memset(cnt, 0, sizeof(cnt));node T;creattree(T,42);printf("Case %d:\n",ans);for(i=min; i<=max; i++){   printf("%d",cnt[i]);   if(i!=max)   printf(" ");    } puts("");puts("");//flag = 0;}return 0;} 



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.