0

I'm pretty new to Python and I have been stuck on this NameError for a while. I thought that it might be a syntax error with the treeRecurse function, which was why I couldn't call the function correctly. But at this point, I'm really lost. Anybody have any ideas?

In place of def treeRecurse (node: TreeNode, ret, L: int, R: int): I had tried def treeRecurse (node, ret, L, R):

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:

    def treeRecurse (node: TreeNode, ret, L: int, R: int):

        if node.val >= L & node.val <= R: ret.append(node.val)

        if node.right != None: treeRecurse(node.right, ret, L, R)
        if node.left != None: treeRecurse(node.left, ret, L, R)

    def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:

        ret = []
        treeRecurse(root, ret, L, R)
        return ret
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 2
    It would be helpful if you actually posted the full error. But I am guessing that you get it for trying to call `treeRecurse` from `rangeSumBST`. There is not actually a function called like that, but an **instance method**. You probably want to write `self.treeRecurse(...)`... – Tomerikoo Jun 08 '20 at 19:24
  • Did you really intend to use the ampersand or did you mean to use `and`? https://stackoverflow.com/questions/22646463/and-boolean-vs-bitwise-why-difference-in-behavior-with-lists-vs-nump –  Jun 08 '20 at 19:52

1 Answers1

3

treeRecurse is a method of the class Solution. There are a couple syntactic requirements that this imposes:

1) The method treeRecurse should have a parameter self as the first parameter, i.e., def treeRecurse (self, node: TreeNode, ret, L: int, R: int):

2) During the recursive call, it should also be called like self.treeRecurse(...).

quamrana
  • 37,849
  • 12
  • 53
  • 71
Jamie
  • 2,181
  • 1
  • 21
  • 35