【codevs1501】二叉树最大宽度和高度

problem

solution

codes

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 110;
int tree[maxn][2], higt, weigt[maxn], ww;
void dfs(int now, int dep){
    higt = max(higt,dep);
    ww = max(ww, weigt[dep]);
    if(tree[now][0]){ dfs(tree[now][0], dep+1); weigt[dep+1]++; }
    if(tree[now][1]){ dfs(tree[now][1], dep+1); weigt[dep+1]++; }
}
int main(){
    int n;  cin>>n;
    for(int i = 1; i <= n; i++)
        cin>>tree[i][0]>>tree[i][1];
    dfs(1, 1);
    cout<<ww+1<<" "<<higt<<"
";
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444706.html