需要Level Order Traversion帮助,收到错误

时间:2018-05-03 04:56:21

标签: c#

我试图弄清楚为什么我从这个hackerrank.com问题(https://www.hackerrank.com/challenges/30-binary-trees/problem)中收到以下错误:

  

solution.cs(32,7):错误CS1525:意外的符号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);

    }
}

1 个答案:

答案 0 :(得分:0)

我将您的代码复制到默认的控制台应用程序中,并且构建时没有错误。

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

        }
    }
}