【问题标题】:Level Order Traversion help needed, getting error需要 Level Order Traversion 帮助,出现错误
【发布时间】: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, 期待 classdelegateenuminterfacepartialstruct 编译失败: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#


【解决方案1】:

我将您的代码复制到了默认的控制台应用程序中,它对我来说构建时没有错误。

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);

        }
    }
}

【讨论】:

  • @EvanTrimboli 指出我的括号太多,提前下课了。不过谢谢你!
猜你喜欢
  • 2016-05-25
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2011-04-02
  • 2012-10-04
  • 1970-01-01
  • 2014-06-04
  • 2021-08-31
相关资源
最近更新 更多