title: How to do tree traversal?
body: <p>So I am running into some issue that has left me dumbfounded. I do not understand where I am going wrong with my code but the idea I have is I check if the current node I am at is <code>None</code> and if it is then I return a list of my tree in <strong>in order</strong>, <strong>pre order</strong>, and <strong>post order</strong>. Here is my code:</p>
  <pre><code>class Node:
    def __init__(self, data):
      self.data = data
      self.left = None
      self.right = None
  
    def inOrder(self, arr=[]):
      if self is not None:
        self.left.inOrder(arr)
        arr.append(self.data)
        self.right.inOrder(arr)
  
      return arr
  </code></pre>
  <p>When I run it I get an error of <code> self.left.inOrder() AttributeError: 'NoneType' object has no attribute 'inOrder'</code> which I have not idea as to why. I am checking that <code>self is not None</code> so shouldn't this guarantee my <code>Node</code> to have a <code>left</code> and <code>right</code>.</p>
  <p>I am only showing the <strong>inOrder</strong> I have implemented.</p>
  <p>I have solved this instead by doing the following</p>
  <pre><code>class Node:
      def __init__(self, data):
          self.data = data
          self.left = None
          self.right = None
  
      def inOrder(self):
          if self is not None and self.left is not None:
              self.left.inOrder()
  
          print(self.data)
  
          if self is not None and self.right is not None:
              self.right.inOrder()
  
  
  root = Node(1)
  root.left = Node(2)
  root.right = Node(3)
  
  
  root.inOrder()
  </code></pre>
  <p>Whether I save it to a list or just print it is fine with me but if I already check if self is not None then shouldn't I be able to call <code>self.left.inOrder</code> and <code>self.right.inOrder</code>?</p>