It keeps producing Error


  • 1
    N

    Cannot implicitly convert type System.Collections.Generic.IEnumerable<System.Collections.Generic.IList<int>>' toSystem.Collections.Generic.IList<System.Collections.Generic.IList<int>>'. An explicit conversion exists (are you missing a cast?)

    public IList<IList<int>> LevelOrderBottom(TreeNode root) 
     { 
        IList<IList<int>> AllNodes = new List<IList<int>>();
         if(root == null)       
     {
            return AllNodes;
     }
         
    IList<int> levelNodes = new List<int>();
    
         Queue<TreeNode> TreeNodes = new Queue<TreeNode>();
    
         TreeNodes.Enqueue(root);
    
         TreeNodes.Enqueue(null);
        
        while (TreeNodes.Count > 0)
        {
            if(TreeNodes.Peek() == null)
            {
                if(TreeNodes.Count > 1)
                {
                    TreeNodes.Enqueue(null);
                }
                AllNodes.Add(levelNodes);
                levelNodes = new List<int>();
            }
            else
            {
                levelNodes.Add(TreeNodes.Peek().val);
                if(TreeNodes.Peek().left!=null)
                {
                    TreeNodes.Enqueue(TreeNodes.Peek().left);
                }
                if(TreeNodes.Peek().right!=null)
                {
                    TreeNodes.Enqueue(TreeNodes.Peek().right);
                }
            }
            TreeNodes.Dequeue();
        }
      IList<IList<int>> RevesredNodes = AllNodes.Reverse();
         return RevesredNodes;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.