18.2.14 codevs1501 二叉樹最大寬度和高度
阿新 • • 發佈:2018-02-14
isp 連接 左右 ron esp color 整數 end codevs
題目描述 Description
給出一個二叉樹,輸出它的最大寬度和高度。
輸入描述 Input Description第一行一個整數n。
下面n行每行有兩個數,對於第i行的兩個數,代表編號為i的節點所連接的兩個左右兒子的編號。如果沒有某個兒子為空,則為0。
輸出描述 Output Description輸出共一行,輸出二叉樹的最大寬度和高度,用一個空格隔開。
樣例輸入 Sample Input5
2 3
4 5
0 0
0 0
0 0
樣例輸出 Sample Output2 3
數據範圍及提示 Data Size & Hintn<16
默認第一個是根節點
以輸入的次序為編號
2-N+1行指的是這個節點的左孩子和右孩子
註意:第二題有極端數據!
1
0 0
這題你們別想投機取巧了,給我老老實實搜索!
1 #include <iostream> 2 #include <math.h> 3 #include<string.h> 4 5 using namespace std; 6 7 int not0[20][3],sum_l[20]={0},lmax=0; 8 9 void tree(intView Codew,int l)//w當前節點 l當前高度 10 { 11 sum_l[l]++; 12 if(not0[w][1]==0&¬0[w][2]==0) 13 { 14 if(lmax<l) 15 lmax=l; 16 } 17 else 18 { 19 if(not0[w][1]!=0) 20 tree(not0[w][1],l+1); 21 if(not0[w][2]!=0) 22 tree(not0[w][2],l+1); 23 } 24 return; 25 } 26 27 int main() 28 { 29 int n; 30 cin>>n; 31 for(int i=1;i<=n;i++) 32 for(int j=1;j<=2;j++) 33 { 34 cin>>not0[i][j]; 35 } 36 tree(1,1); 37 int max=0; 38 for(int i=1;i<=n;i++) 39 { 40 if(max<sum_l[i]) 41 max=sum_l[i]; 42 } 43 cout<<max<<" "<<lmax<<endl; 44 return 0; 45 }
查了一下二叉樹的概念和二叉樹寬度指的是什麽(....
二叉樹寬度:具有最多結點數的層中包含的結點數
18.2.14 codevs1501 二叉樹最大寬度和高度