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