分值 : 30 难度 : 中等 思路 : 二叉树建树,然后层序遍历 坑点 : 以为自己忘记了,实际上没有,也许这个在脑子里扎了根了吧....一遍下来挺顺畅的
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
#include <iostream>using namespace std;typedef struct Node{ int self ; Node* l; Node* r; int level=0;}Nodes ;Nodes data[1001];Node* insert(Nodes* pos , int value){ if(pos==NULL) { Nodes* temp = (Nodes*)malloc(sizeof(Node)); temp->self = value; temp->l =NULL ; temp->r =NULL ; pos = temp ; return pos ; } if(value <= pos->self) pos->l = insert(pos->l, value); else if(value > pos->self) pos->r = insert(pos->r ,value) ; return pos ;}int main() { int N ; cin >> N ; int temp ; Nodes* head = NULL ; for(int i = 0 ; i< N ; i++) { cin >> temp ; head = insert(head ,temp) ; } Nodes* Queen[N] ; int cur = 0 ,count = 0 ; Queen[count++] = head; int now = 0 ; while(cur < count ) { now = Queen[cur]->level ; if(Queen[cur]->l) { Queen[count] = Queen[cur]->l ; Queen[count++]->level = now+1 ; } if(Queen[cur]->r) { Queen[count] = Queen[cur]->r ; Queen[count++]->level = now+1 ; } cur ++ ; } int n1= 0,n2=0 ; for(int i = 0 ; i< cur ; i++) { if(Queen[i]->level == now - 1 ) n2++ ; if(Queen[i]->level == now ) n1 ++ ; } cout << n1 <<" + "<<n2<<" = "<<n1+n2 <<endl ;}