题目意思:
给你一组排序二叉树,让你输出节点的方向,小的是E,大的是W
CODE
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
using namespace std;
struct node
{
int x;
struct node *l, *r;
};
char mp[1010][1010];
node *head;
void juge()///小的是右边E
{
node *tail,*q;
q = new node;
scanf("%d",&q->x);
q->l = NULL;
q->r = NULL;
tail = head;
int cnt = 0;
while(tail)
{
if(tail->x > q->x)
{ mp[q->x][cnt++] = 'E';
if(tail->r)
tail = tail->r;
else
{
tail->r = q;
return;
}
}
else
{ mp[q->x][cnt++] = 'W';
if(tail->l)
tail = tail->l;
else
{
tail->l = q;
return;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(mp,'\0',sizeof(mp));
int n;
scanf("%d",&n);
int i;
head = new node;
scanf("%d",&head->x);
head->l = NULL;
head->r = NULL;
for(i = 1; i < n; i++)
{
juge();
}
int m;
scanf("%d",&m);
for(i = 0; i < m; i++)
{
int b;
cin>>b;
cout<<mp[b]<<endl;
}
}
return 0;
}

本文介绍了一种算法,用于判断排序二叉树中节点的方向,通过输入节点值,输出小值为E(右边),大值为W(左边)。算法通过循环遍历树结构,根据节点值与当前节点值的比较结果决定方向,并更新路径。

被折叠的 条评论
为什么被折叠?



