C
C#2y ago
morry329#

✅ A bunch of syntax errors

I am trying to solve this puzzle https://leetcode.com/problems/n-ary-tree-preorder-traversal/ I got a couple of syntax errors at the foreach loop (like I am missing ; or , etc), but I have no idea how to apply the variable val assigned by LeetCode. Could anyone kindly point me some directions?
public class Solution {
public IList<int> Preorder(Node root) {
List<int> ans = new List<int>();
if(root == null){
return ans;
}
Helper(root, ans);
return ans;
}

public void Helper(Node root, List<int> ans){

foreach(Node root.value in root.children){
Helper(root, ans);
}
}
}
public class Solution {
public IList<int> Preorder(Node root) {
List<int> ans = new List<int>();
if(root == null){
return ans;
}
Helper(root, ans);
return ans;
}

public void Helper(Node root, List<int> ans){

foreach(Node root.value in root.children){
Helper(root, ans);
}
}
}
`
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.
8 Replies
mtreit
mtreit2y ago
foreach(Node root.value in root.children)
foreach(Node root.value in root.children)
Well that doesn't look right for one thing root.value is not a variable. foreach should be doing something like:
foreach (Node node in root.children)
{
var val = node.value;
var children = node.children;
// Do stuff
}
foreach (Node node in root.children)
{
var val = node.value;
var children = node.children;
// Do stuff
}
or something like that.
morry329#
morry329#2y ago
Thanks a lot for your guide 🙏 I will try that again
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.
morry329#
morry329#2y ago
One more question - my current code is like this right now
public class Solution {
public IList<int> Preorder(Node root) {
List<int> ans = new List<int>();
if(root == null){
return ans;
}
Helper(root, ans);
return ans;
}

public void Helper(Node root, List<int> ans){

foreach(Node node in root.children){
if (node.val == root.val){
Helper(node, ans);
Helper(root, ans);
}
}
}


}
public class Solution {
public IList<int> Preorder(Node root) {
List<int> ans = new List<int>();
if(root == null){
return ans;
}
Helper(root, ans);
return ans;
}

public void Helper(Node root, List<int> ans){

foreach(Node node in root.children){
if (node.val == root.val){
Helper(node, ans);
Helper(root, ans);
}
}
}


}
` And I got null array [] returned instead of the pre-ordered array Could you kindly give me some hints again as to why my code returns something so null?
333fred
333fred2y ago
You never add anything to the list
mtreit
mtreit2y ago
[] means empty not null
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.
morry329#
morry329#2y ago
Thank you so much - I managed to get it solved Here is my code
public class Solution {
public IList<int> Preorder(Node root) {
List<int> ans = new List<int>();
if(root == null){
return ans;
}
ans.Add(root.val);
Helper(root, ans);
return ans;

}

public void Helper(Node root, List<int> ans){
foreach(var node in root.children){


ans.Add(node.val);

Helper(node,ans);
}
}
}
public class Solution {
public IList<int> Preorder(Node root) {
List<int> ans = new List<int>();
if(root == null){
return ans;
}
ans.Add(root.val);
Helper(root, ans);
return ans;

}

public void Helper(Node root, List<int> ans){
foreach(var node in root.children){


ans.Add(node.val);

Helper(node,ans);
}
}
}
`
Want results from more Discord servers?
Add your server
More Posts