title: Finding the depth of a particular node in a tree using recursion
body: <p>I'm currently trying to solve a problem where I return the maximum depth a number appears in a tree. For example, if a tree looks like this:</p>
  
  <pre><code>    1
    /   \
  2      3
          \
           2
  </code></pre>
  
  <p>My function should return 2. Yet, my function returns 0</p>
  
  <pre><code>def max_depth(t,value):
      if t == None:
          return -1
      left = max_depth(t.left, value)
      right = max_depth(t.right, value)
      if t.value == value:
          return 1 + max(left,right)
      else:
          return max(left,right)
  </code></pre>
  
  <p>Is my thought process wrong? I should add <code>1</code> if the current value matches the one I'm looking for (which is the parameter), and do not add <code>1</code> if they do not match.  I use <code>max()</code> so it returns the maximum of either the left child or the right child, so I get the child with the higher depth. Is that wrong?</p>
  
  <p>Here is the tree class:</p>
  
  <pre><code>class TN:
      def __init__(self,value,left=None,right=None):
          self.value = value
          self.left  = left
          self.right = right
  </code></pre>
  
  <p>And here is my construction of the tree:</p>
  
  <pre><code>tree4 = TN(2)
  tree3 = TN(3, left = None, right = tree4)
  tree2 = TN(2)
  tree1 = TN(1, left = tree2, right = tree3)
  print(max_depth(tree1, 2))
  </code></pre>
  
  <p>That will print <code>0</code></p>