題目描述 Description
給出一個二叉樹,輸出它的最大寬度和高度。
輸入描述 Input Description
第一行一個整數n。
下面n行每行有兩個數,對于第i行的兩個數,代表編号為i的節點所連接配接的兩個左右兒子的編号。如果沒有某個兒子為空,則為0。
輸出描述 Output Description
輸出共一行,輸出二叉樹的最大寬度和高度,用一個空格隔開。
樣例輸入 Sample Input
5
2 3
4 5
0 0
0 0
0 0
樣例輸出 Sample Output
2 3
資料範圍及提示 Data Size & Hint
n<16
預設第一個是根節點
以輸入的次序為編号
2-N+1行指的是這個節點的左孩子和右孩子
注意:第二題有極端資料!
1
0 0
這題你們别想投機取巧了,給我老老實實搜尋!
#include <iostream>
#include <string.h>
using namespace std;
int x=0;
int temp[16];
int a[16][2];
void binary(int i,int k)
{
temp[k-1]++;
if(k>x) x=k;
if(a[i][0]!=0) binary(a[i][0],k+1);
if(a[i][1]!=0) binary(a[i][1],k+1);
}
int main()
{
int t=0;
memset(temp,0,16);
int n,i;
cin>>n;
for(i=1;i<n+1;i++)
{
cin>>a[i][0]>>a[i][1];
}
binary(1,1);
for(i=0;i<n;i++)
{
if(temp[i]>t)
t=temp[i];
}
cout<<t<<" "<<x;
return 0;
}