title: calculating depth of a binary tree in Python
body: <p>I am new to programming and am trying to calculate the depth of a binary tree in Python . I believe that my error is because depth is a method of the Node class and not a regular function. I am trying to learn OOP and was hoping to use a method. This might be a newbie error...
  Here is my code:</p>
  
  <pre><code>class Node:
  
      def __init__(self, item, left=None, right=None):
          """(Node, object, Node, Node) -&gt; NoneType
          Initialize this node to store item and have children left and right.
          """
          self.item = item
          self.left = left
          self.right = right
  
      def depth(self):
          if self.left == None and self.right == None:
              return 1
  
          return max(depth(self.left), depth(self.right)) + 1
  
  i receive this error:
  
  &gt;&gt;&gt;b = Node(100)
  
  &gt;&gt;&gt;b.depth()
  
  1 
  
  &gt;&gt;&gt;a = Node(1, Node(2), Node(3))
  
  &gt;&gt;&gt;a.depth()
  
  Traceback (most recent call last):
    File "C:\Program Files\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 1, in &lt;module&gt;
      # Used internally for debug sandbox under external interpreter
    File "C:\Program Files\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 15, in depth
  builtins.NameError: global name 'depth' is not defined
  </code></pre>