C
C#2y ago
morry329#

❔ Two errors I cannot understand

I am trying to create a solution for this LC puzzle https://leetcode.com/problems/binary-tree-level-order-traversal/?envType=study-plan&id=level-1 My solution has a couple of errors like this;
public class Solution {
public IList<IList<int>> LevelOrder(TreeNode root) {
List<IList<int>> result = new();
if(root == null){
return result;
}

var q = new Queue<(int level, TreeNode node)>();
var separation = new List<IList<int>>();

q.Enqueue((0, root));
while(q.Count > 0){
var curr = q.Dequeue();
if(separation.Count <= curr.level){
separation.Add(new List<IList<int>>()); //ERROR 1
}
separation[curr.level].Add(curr.node.val);

curr.level++;
if(curr.node.left != null){
q.Enqueue((level, curr.node.left)); // ERROR 2
}

if(curr.node.right != null){
q.Enqueue((level, curr.node.right)); //ERROR 3
}
result.Add(separation); //ERROR 4
}
return result;


}
}
public class Solution {
public IList<IList<int>> LevelOrder(TreeNode root) {
List<IList<int>> result = new();
if(root == null){
return result;
}

var q = new Queue<(int level, TreeNode node)>();
var separation = new List<IList<int>>();

q.Enqueue((0, root));
while(q.Count > 0){
var curr = q.Dequeue();
if(separation.Count <= curr.level){
separation.Add(new List<IList<int>>()); //ERROR 1
}
separation[curr.level].Add(curr.node.val);

curr.level++;
if(curr.node.left != null){
q.Enqueue((level, curr.node.left)); // ERROR 2
}

if(curr.node.right != null){
q.Enqueue((level, curr.node.right)); //ERROR 3
}
result.Add(separation); //ERROR 4
}
return result;


}
}
` ERROR 1: Argument 1: cannot convert from 'System.Collections.Generic.List<System.Collections.Generic.IList<int>>' to 'System.Collections.Generic.IList<int>' ERRORS 2 & 3: The name 'level' does not exist in the current context (in Solution.cs) ERROR 4: Argument 1: cannot convert from 'System.Collections.Generic.List<System.Collections.Generic.IList<int>>' to 'System.Collections.Generic.IList<int>' (in Solution.cs) Could anyone kindly point me in the right direction?
LeetCode
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
5 Replies
Angius
Angius2y ago
Well, errors 1 and 4 are plenty clear Something expects an apple You're trying to cram a container of apples into it Square peg doesn't go into round hole 2 and 3 are quite clear as well There's no level wherever that error occurs You can't reference something that does not exist
MODiX
MODiX2y ago
you probably want $scopes
Angius
Angius2y ago
$scopes
MODiX
MODiX2y ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server