0

I have this Function:

def depth(self):
        if self.root == None:
            return 0
        left = depth(self.left)
        right = depth(self.right)
        if left>right:
            return left+1
        else:
            return right+1

But when I run it, the following error pops up:

 File "/Users/suryakannan/Documents/Python Programs/binaryTree.py", line 60, in depth
    left = depth(self.left)
NameError: name 'depth' is not defined

So, what should I do to fix this function?

2
  • 1
    Try self.depth(self.lenght), Commented Mar 21, 2019 at 1:31
  • 1
    shouldn't it be self.depth since it's inside a class Commented Mar 21, 2019 at 1:31

1 Answer 1

2

Since depth is an instance method, you need to use the self reference

def depth(self):
    if self.root == None:
        return 0
    left = self.depth(self.left)
    right = self.depth(self.right)
    if left>right:
        return left+1
    else:
        return right+1
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much for helping me. I really appreciate everyone that contributed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.