title: Recursively find arbitrary node path in tree
body: <p>I have a tree of objects composed with the following structure:</p>
  
  <pre><code>interface Node {
    name: string;
    children?: Node[];
  }
  </code></pre>
  
  <p>Where, for example, <code>{ name: "foo", children: [ { name: "bar" } ] }</code> is a valid tree. I'm trying to get the address of an arbitrary node, that can be a leaf, returned as an array of the path to arrive it, where the node will be compared by object reference. Example given, I have the following structure:</p>
  
  <pre><code>var data = {
    name: "Languages",
    children: [{
      name: "Functional",
      children: [
        { name: "OCaml" },
        { name: "Haskell" },
        { name: "Erlang" }
      ]
    }, {
      name: "Imperative",
      children: [
        { name: "BASIC" },
        { name: "Clipper" }
      ]
    }]
  };
  </code></pre>
  
  <p>It is expected that, for example, if I have a reference for <code>OCaml</code> stored in a variable called <code>ocaml</code> that points exactly to that reference in memory, an array of steps be returned with the path to reach it, that would be, in this case, <code>[0, 0]</code>.</p>
  
  <p>I already can locate the object, but I'm not able to store the previous index, because this is non-deterministic:</p>
  
  <pre><code>var tree = function(struct, cmp) {
    if (struct.children) {
      var lookup = [];
      for (var i = 0; i &lt; struct.children.length; i++) {
        lookup.push(tree(struct.children[i], cmp));
      }
      return lookup;
    } else {
      if (struct === cmp) {
        return "PASS";
      }
      return "FAIL";
    }
  }
  </code></pre>
  
  <p>I can find the element, but how can I store the previous indexes to reach it from the base?</p>