【发布时间】:2018-05-03 13:09:28
【问题描述】:
我试图弄清楚为什么我从这个 hackerrank.com 问题 (https://www.hackerrank.com/challenges/30-binary-trees/problem) 中得到以下错误:
solution.cs(32,7): error CS1525: Unexpected symbol
Node, 期待class、delegate、enum、interface、partial或struct编译失败: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);
}
}
【问题讨论】:
-
您的静态
insert方法不属于任何类。 -
@EvanTrimboli 谢谢!我现在调整一下
-
@EvanTrimboli 就是这样!显然,我的括号太多了,我想我需要过早地结束课程。谢谢。
-
@EvanTrimboli 你能把它作为答案提交给你吗?
-
建议您删除它,我认为这个问题没有太大价值。
标签: c#