C# 需要级别顺序遍历帮助,获取错误
我正在试图找出为什么我会从这个hackerrank.com问题()中得到以下错误: solution.cs(32,7):错误CS1525:意外的符号C# 需要级别顺序遍历帮助,获取错误,c#,C#,我正在试图找出为什么我会从这个hackerrank.com问题()中得到以下错误: solution.cs(32,7):错误CS1525:意外的符号节点,应为 类,委托,枚举,接口,部分,或结构 编译失败:1个错误,0个警告 退出状态:255 我已经检查了所有常用的东西,比如分号和花括号,但是我被难住了 我的代码如下所示,我已经对错误的起始位置进行了注释: using System; using System.Collections; using System.Collections.Gener
节点
,应为
类
,委托
,枚举
,接口
,部分
,或结构
编译失败:1个错误,0个警告
退出状态:255
我已经检查了所有常用的东西,比如分号和花括号,但是我被难住了
我的代码如下所示,我已经对错误的起始位置进行了注释:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Node{
public Node left,right;
public int data;
public Node(int data){
this.data=data;
left=right=null;
}
}
class Solution{
static Queue<Node> nodeQueue = new Queue<Node>();
static void levelOrder(Node root){
//Write your code here
nodeQueue.Enqueue(root);
while (nodeQueue.Count > 0){
var n = nodeQueue.Dequeue();
Console.Write(n.data + " ");
if (n.left != null) {
nodeQueue.Enqeue(n.left);
}
if (n.right != null) {
nodeQueue.Enqueue(n.right);
}
}
}
}
// Right here is where it is saying it's not expecting "NODE", the line below
static Node insert(Node root, int data){
if(root==null){
return new Node(data);
}
else{
Node cur;
if(data<=root.data){
cur=insert(root.left,data);
root.left=cur;
}
else{
cur=insert(root.right,data);
root.right=cur;
}
return root;
}
}
static void Main(String[] args){
Node root=null;
int T=Int32.Parse(Console.ReadLine());
while(T-->0){
int data=Int32.Parse(Console.ReadLine());
root=insert(root,data);
}
levelOrder(root);
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
类节点{
公共节点左、右;
公共int数据;
公共节点(int数据){
这个。数据=数据;
左=右=空;
}
}
类解决方案{
静态队列nodeQueue=新队列();
静态void levelOrder(节点根){
//在这里编写代码
nodeQueue.Enqueue(根);
而(nodeQueue.Count>0){
var n=nodeQueue.Dequeue();
Console.Write(n.data+“”);
如果(n.left!=null){
诺德奎埃·恩奎埃(北左);
}
如果(n.right!=null){
nodeQueue.Enqueue(右);
}
}
}
}
//就在这里,它说它不需要“节点”,下面的一行
静态节点插入(节点根,int数据){
if(root==null){
返回新节点(数据);
}
否则{
节点电流;
如果(数据0){
int data=Int32.Parse(Console.ReadLine());
根=插入(根,数据);
}
levelOrder(根);
}
}
我将您的代码复制到一个默认控制台应用程序中,并为我构建了一个没有错误的应用程序
using System;
namespace ConsoleApp2
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Node
{
public Node left, right;
public int data;
public Node(int data)
{
this.data = data;
left = right = null;
}
}
class Solution
{
static Queue<Node> nodeQueue = new Queue<Node>();
static void levelOrder(Node root)
{
//Write your code here
nodeQueue.Enqueue(root);
while (nodeQueue.Count > 0)
{
var n = nodeQueue.Dequeue();
Console.Write(n.data + " ");
if (n.left != null)
{
nodeQueue.Enqueue(n.left);
}
if (n.right != null)
{
nodeQueue.Enqueue(n.right);
}
}
}
// Right here is where it is saying it's not expecting "NODE", the line below
static Node insert(Node root, int data)
{
if (root == null)
{
return new Node(data);
}
else
{
Node cur;
if (data <= root.data)
{
cur = insert(root.left, data);
root.left = cur;
}
else
{
cur = insert(root.right, data);
root.right = cur;
}
return root;
}
}
static void Main(String[] args)
{
Node root = null;
int T = Int32.Parse(Console.ReadLine());
while (T-- > 0)
{
int data = Int32.Parse(Console.ReadLine());
root = insert(root, data);
}
levelOrder(root);
}
}
}
使用系统;
名称空间控制台EApp2
{
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
类节点
{
公共节点左、右;
公共int数据;
公共节点(int数据)
{
这个数据=数据;
左=右=空;
}
}
类解决方案
{
静态队列nodeQueue=新队列();
静态void levelOrder(节点根)
{
//在这里编写代码
nodeQueue.Enqueue(根);
而(nodeQueue.Count>0)
{
var n=nodeQueue.Dequeue();
Console.Write(n.data+“”);
如果(n.left!=null)
{
nodeQueue.Enqueue(n.左);
}
如果(n.right!=null)
{
nodeQueue.Enqueue(右);
}
}
}
//就在这里,它说它不需要“节点”,下面的一行
静态节点插入(节点根,int数据)
{
if(root==null)
{
返回新节点(数据);
}
其他的
{
节点电流;
如果(数据0)
{
int data=Int32.Parse(Console.ReadLine());
根=插入(根,数据);
}
levelOrder(根);
}
}
}
您的staticinsert
方法不是任何类的一部分。@Evantimboli谢谢!我会调整它的now@EvanTrimboli就是这样!显然我的括号太多了,我认为我需要提前结束课程。谢谢。@Evantimboli你能把它作为一个答案提交给我,这样我就可以给你学分了吗?建议你删除它它,我不认为这个问题有多大价值。@Evantimboli指出我的括号太多了,提前结束了课程。不过谢谢你!