1501 二叉树最大宽度和高度 (BFS+树的遍历)
题目:http://www.wikioi.com/problem/1501/
给你一颗二叉树,求该数的宽和高,
首先求出树的高,直接进行二叉树遍历,能够得到二叉树的高
然后是得到宽,本人采用的是一层一层的遍历,求出每一层节点的个数,最大的则是该树的宽了,
代码:
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <iostream> using namespace std; #define L 1
#define R 2
class tree{
private:
int a[][],h,w;
stack<int> mystack;
public:
tree();
void set_tree(int i,int x,int y);
void tree_h(int i,int t);//得到高
void BFS(int t);//得到宽
int get_h();
int get_w();
}; tree::tree()
{
memset(a,,sizeof(a));
h = ;
w = ;
} void tree::set_tree(int i,int x,int y)
{
a[i][L] = x;
a[i][R] = y;
}
int tree::get_h()
{
int t = ;
tree::tree_h(a[][L],t+);
tree::tree_h(a[][R],t+);
return h-;
}
int tree::get_w()
{
mystack.push();
tree::BFS();
return w;
}
void tree::BFS(int i)
{
if(i <= h)
{
int t = ,x;
stack<int> tmpstack;
while(mystack.empty() == false)
{
x = mystack.top();
mystack.pop();
if(x != )
{
t++;
tmpstack.push(a[x][L]);
tmpstack.push(a[x][R]);
}
} w = w > t ? w : t;
mystack = tmpstack;
tree::BFS(i+);
}
} void tree::tree_h(int i,int t)
{
if(i == )
{
h = h > t ? h : t;
return;
}
else
{
tree::tree_h(a[i][L],t+);
tree::tree_h(a[i][R],t+);
}
}
int main()
{
int n,x,y;
while(~scanf("%d",&n))
{
tree mytree;
for(int i = ; i <= n; i++)
{
scanf("%d%d",&x,&y);
mytree.set_tree(i,x,y);
}
int h = mytree.get_h();
int w = mytree.get_w();
printf("%d %d\n",w,h);
}
return ;
}
1501 二叉树最大宽度和高度 (BFS+树的遍历)的更多相关文章
- codevs——1501 二叉树最大宽度和高度
1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 给出一个二叉树,输出它的 ...
- codevs 1501 二叉树最大宽度和高度x
题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整数n. 下面n行每行有两 ...
- AC日记——二叉树最大宽度和高度 1501 codevs
1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽 ...
- Codevs 1051 二叉树最大宽度和高度
1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽 ...
- codevs1501 二叉树最大宽度和高度
难度等级:白银 1501 二叉树最大宽度和高度 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整数n. 下面 ...
- ACM题目————二叉树最大宽度和高度
http://codevs.cn/problem/1501/ 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整 ...
- [wikioi]二叉树最大宽度和高度
简单的DFS,用数组w记录每一层的宽度就行了,就是遇到一层就++.中间发现在C++里面,如果int未初始化就是用也是有异常的.还有二叉树的数组表示时,从1开始计数会比较好.还有后来学会了数组这样的初始 ...
- Codevs 1501 二叉树的最大宽度和高度
1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描 ...
- 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离
数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...
随机推荐
- SQL数据库知识二(Day 25)
又到了总结知识的时候了,今天主要把SQL数据库给简单的学完了,明天开始就要开始学ADO.NET的知识了.好了,话不多说,还是看一下今天都学了哪些内容. 1 字符串类型的知识点 --类型的使用 --截 ...
- Android DatePicker和TimePicker
监测日期改变的监听器: OnDateChangedListener和OnTimeChangedListener() 当用户改变Datepicker里的年.月.日时,将触发 ...
- mysql函数操作(3)
<?php $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); $dbh->s ...
- Eclipse Rcp
http://blog.csdn.net/soszou/article/details/7996748
- HDU 5809 Ants(KD树+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5809 [题目大意] 给出一些蚂蚁和他们的巢穴,一开始他们会在自己的巢穴(以二维坐标形式给出),之后 ...
- 编译时出现clock skew detected, your build may be incompeleted
错误原因为文件修改时间大于系统时间,这时候如果date输出系统时间,会发现这个时间是错误的.在nachos实习时多次出现这个错误,简单的方法尝试make多次直到有一次出现'nachos' is up ...
- poj2509---抽k根烟就换一支,求能抽烟的总数
#include <stdio.h> #include <stdlib.h> int main() { int now,k; while(scanf("%d %d&q ...
- 通过crash了解linux页表
目的: 通过一个c语言实例,了解linux页表的组织结果和mmu的工作原理. 通过页表找到一个物理地址, 对比物理地址与虚拟地址的内容是否一致. 运行环境: $ uname -r3.15.6-200. ...
- leetcode_question_85 Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Hotel(线段树合并)
Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14958 Accepted: 6450 Descriptio ...