[Problem]
给出一个二叉树,输出它的最大宽度和高度。
[Solution]
#include<iostream> using namespace std; int n; int l[20], r[20], w[20], deep; void dfs(int x, int y){ w[y]++; if(y > deep) deep = y; if(l[x]) dfs(l[x], y+1); if(r[x]) dfs(r[x], y+1); } int main(){ cin >> n; int i; for(i = 1; i <= n; i++) cin >> l[i] >> r[i]; dfs(1, 1); int mw = 0; for(i = 1; i <= deep; i++) mw = max(mw, w[i]); cout << mw << " " << deep; }