title: Python finding paths of trees using recursion
body: <p>I've been trying to implement an Huffman encoding algorithm based on numeric digits. I've done the part to construct an Huffman tree. But the recursion algorithm doesn't work as expected. It should return the paths in the tree from the root to the designated node, but it always return the wrong path. </p>
  
  <p>The strange thing is, the code seems to be doing what is correct, and it can find the real path. But the result it returns is always something else.</p>
  
  <pre><code>def get_encoding_for(symbol_p, node, encoding):
      global encoded_string
      result = encoding
      if not isinstance(node, list):
          if symbol_p == node:
              encoded_string = result
              return result
      else:
          for n in node:
              index = str(node.index(n))
              # print("Node {} with index {}".format(n, index))
              result = get_encoding_for(symbol_p, n, encoding + index)
  
      return result
  </code></pre>
  
  <p>The tree is structured using simply lists.</p>
  
  <pre><code>The huffman tree looks like: [[3.0, [1.0, 2.0]], [4.0, 5.0]]
  </code></pre>
  
  <p>This is an example output of a simple tree using elements 1,2,3,4,5.</p>
  
  <pre><code>Loop 1.0. Node 1.0 -&gt; Coding 010:
  Loop 2.0. Node 2.0 -&gt; Coding 011:
  Loop 3.0. Node 3.0 -&gt; Coding 00:
  Loop 4.0. Node 4.0 -&gt; Coding 10:
  Loop 5.0. Node 5.0 -&gt; Coding 11:
  </code></pre>
  
  <p>This is what I want the function to return, but it only returns me "11" in all the five iterations. I had to use a global variable to "intercept" the correct coding and I'm not happy with this... I think the problem is on the returning. I've tried a lot of ways of returning but none of them worked.</p>
  
  <p>Can someone tells me what's wrong about the recursion? Thank you so much!</p>