wikioi天梯之1501 二叉树最大宽度和高度

来源:互联网 发布:淘宝视频短片拍摄教程 编辑:程序博客网 时间:2024/05/29 03:35

题目

深度优先搜索的又一个例子

#include <iostream>using namespace std;int tree[20][2]; //测试数据小于16.。。。全局数组默认初始化为0 所以不用初始化了微笑int depth = 0,widh[40];//void find_DP(int k,int floor)//floor 记住搜索的层数{    if(floor > depth)depth = floor;//保存搜索过的最大的层数    widh[floor]++;    if(tree[k][0] != 0)find_DP(tree[k][0],floor + 1);    if(tree[k][1] != 0)find_DP(tree[k][1],floor + 1);}int main(){    int n,max;    cin>>n;    for(int i = 1;i <= n;i++)//最好从一开始存 要不然很容易搞乱。。。    cin>>tree[i][0]>>tree[i][1];    find_DP(1,1);    max = widh[1];    for(int i = 2;i < 40;i++)    if(max < widh[i])max = widh[i];    cout<<max<<" "<<depth;    return 0;}


0 0