此题摘录各界大佬的思路与代码
555我是个笨蛋
这道基操题就卡了好几天
“慢慢来是很好的诚意。”
Description
二叉树的宽度指的是具有节点数目最多的那一层的节点个数。 1 / \ 2 3 / 4 答案为2, 第二层节点数最多,为2个节点。
输入格式
共n行。 第一行一个整数n,表示有n个结点,编号为1至n,结点1为树根。(1<=n<=50) 第二行至第n行,每行有两个整数x和y,表示在二叉树中x为y的父节点。x第一次出现时y为左孩子输出格式
输出二叉树的宽度。输入样例
5 1 2 1 3 2 4 2 5输出样例
2
首先上OJ标程!
①标程就是简单大气!(BFS)
#include <iostream>
#include <queue>
using namespace std;
int n,child[105][3],ans=1;
int main()
{
ios::sync_with_stdio(false);
int x,y;
cin>>n;
for(int i=1;i<n;i++)
{
cin>>x>>y;
if(!child[x][1])
child[x][1]=y;
else
child[x][2]=y;
}
queue<int>q;
q.push(1);
while(q.size())
{
int len=q.size();
ans=max(ans,len);
for(int i=0;i<len;i++)
{
int e;
e=q.front();
q.pop();
if(child[e][1])
q.push(child[e][1]);
if(child[e][2])
q.push(child[e][2]);
}
}
cout<<ans;
return 0;
}
② 扩展一哈,再熟悉一下二叉树基操吧(带求深度)
#include <iostream>
#include <queue>
using namespace std;
struct BTNode
{
char value;
BTNode *lchild;
BTNode *rchild;
};
/**< 先序创建二叉树 */
void CreatBtree(BTNode *&root)
{
char ch=0;
cin>>ch;
if(ch=='#')
{
return;
}
else
{
root=new BTNode();
root->value=ch;
CreatBtree(root->lchild);
CreatBtree(root->rchild);
}
}
/**< 求二叉树的深度 */
int GetDepth(BTNode *proot)
{
if(proot==NULL)
return 0;
return GetDepth(proot->lchild)>GetDepth(proot->rchild)?(GetDepth(proot->lchild)+1):(GetDepth(proot->rchild)+1);
}
/**< 求二叉树的宽度 */
int GetWidth(BTNode *proot)
{
if(proot==NULL)
return 0;
int LastLevelWidth=0;
int TempLastWidth=0;
int CurLevelWidth=0;
int width=0;//二叉树的宽度
queue<BTNode *>q;
q.push(proot);//根节点入队,用size
LastLevelWidth=1;
BTNode *pcur=NULL;
while(!q.empty())
{
TempLastWidth=LastLevelWidth;
while(TempLastWidth!=0)
{
pcur=q.front();
q.pop();
if(pcur->lchild!=NULL)
{
q.push(pcur->lchild);
}
if(pcur->rchild!=NULL)
{
q.push(pcur->rchild);
}
TempLastWidth--;
}
CurLevelWidth=q.size();//就是当前结点层的宽度
width=CurLevelWidth>width?CurLevelWidth:width;
LastLevelWidth=CurLevelWidth;
}
return width;
}
③ 然后看看二叉树宽度的一题多解(加分DNA动了!!)
#include <iostream>
#include <queue>
using namespace std;
typedef struct node
{
char value;
struct node* lchild;
struct node* rchild;
}TreeNode,*Tree;
/**< 非递归求宽度 */
int GetWidth(Tree &t)
{
if(t==0)
return 0;
int max=0;
queue<TreeNode*>q;
q.push(t);
while(!q.empty())
{
int CurWidth=q.size();
for(int i=0;i<CurWidth;i++)//一层的结点宽度
{
Tree s=q.front();
q.pop();
if(s->lchild)
q.push(s->lchild);
if(s->rchild)
q.push(s->rchild);
}
max=max>CurWidth?max:CurWidth;//更新宽度
}
return max;
}
/**< 递归计算二叉树宽度 */
void widthRecur(Tree &t,int k,int *width,int &max)
{
if(t==NULL)
return;
width[k]++;//对应层数宽度是1
max=max>width[k]?max:width[k];
widthRecur(t->lchild,k+1,width,max);
widthRecur(t->rchild,k+1,width,max);
}
/**< 先序创建 */
void CreatTree(Tree &t)
{
char x;
cin>>x;
if(x=='#')
t=NULL;
else
{
t=new TreeNode;
t->value=x;
CreatTree(t->lchild);
CreatTree(t->rchild);
}
}
void levelOrder(Tree &t)
{
if(t==NULL)
return;
queue<Tree>q;
q.push(t);
while(!q.empty())
{
int n=q.size();
for(int i=0;i<n;i++)
{
TreeNode *s=q.front();
cout<<s->value<<" ";
q.pop();
if(s->lchild)
q.push(s->lchild);
if(s->rchild)
q.push(s->rchild);
}
cout<<endl;
}
}
int main()
{
Tree t;
CreatTree(t);
levelOrder(t);
int max=0;
int width[3]={0};
cout<<endl<<"递归:"<<endl;
widthRecur(t,1,width,max);
cout<<max;
cout<<endl<<"非递归:"<<endl;
cout<<GetWidth(t);
}
④ copy一下下大佬的代码(主要还是BFS)“ 学习新思想,争做新青年”
#include <iostream>
#include <algorithm>
using namespace std;
const int M=1e5+5;
const int INF=0x3f3f3f3f;
struct node
{
int lch,rch;
};
struct node q[M];
int main()
{
ios::sync_with_stdio(false);
int n,x,y;cin>>n;
/*初始化数组*/
for(int i=0;i<n;i++)
{
q[i].lch=q[i].rch=0;
}
for(int i=1;i<n;i++)
{
cin>>x>>y;
if(q[x].lch==0)
{
q[x].lch=y;
}
else
{
q[x].rch=y;
}
}
/*建树*/
/*队列初始化*/
int a[M];
int head=0,tail=0,ans=-INF;int len=0;
a[tail]=1;
tail++;
while(head<tail)
{
len=tail-head;/*代表当前层的宽度,方便我们扩展节点*/
ans=max(ans,len);
for(int i=1;i<=len;i++)
{
int e=a[head++];/*一个节点只能扩展一次*/
if(q[e].lch!=0)
{
a[tail++]=q[e].lch;
}
if(q[e].rch!=0)
{
a[tail++]=q[e].rch;
}
}
}
cout<<ans<<'\n';
return 0;
}
✊✊✊