I'm doing the n-ary tree preorder traversal on leetcode.
I have this solution that uses two methods:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution(object):
def preorder(self, root):
output = []
self.recur(root, output)
return output
def recur(self, root, output):
if not root:
return []
output.append(root.val)
for x in root.children:
self.recur(x, output)
I was wondering how to do the same recursion using one method. The solution below gives me problems. Namely, when leetcode is running testcases, it aggregates the output. I.e. the output array for test case #2 is appended to test case #1's out put array.
So: Testcase 1 output = [Test 1 solution] Testcase 2 output = [Test 1 solution, Test Case 2 solution]
test = []
class Solution(object):
def preorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
if not root:
return []
test.append(root.val)
for x in root.children:
self.preorder(x)
return test
You should likely not be using global variables as part of this leetcode challenge, as leetcode is likely creating a new class instance of
Solutionfor each test case. The easiest way to resolve this would be to create a class constructor that initializes your list. This will remove the global variable which is causing the issues you've mentioned.