title: OCaml - Mirror image of a tree
body: <p>My tree type is</p>
  
  <pre><code>    type 'a tree = Tree of 'a * 'a tree list;; 
  </code></pre>
  
  <p>How can I get the mirror image of a tree like this? For me is confusing having a list of children, because I do not know how to get individually to each child and do the recursion whitout losing the parent's track, any idea?</p>
  
  <p>EDITED:
  I have kept trying and I think I got the solution:</p>
  
  <pre><code>    let spec arbol =
           match arbol with
           Tree(a,[])-&gt; Tree(a,[])
           | Tree(a,l)-&gt; Tree(a, List.rev (
                    let rec aux = function
                        Tree(a,[])::[]-&gt;Tree(a,[])::[]
                        | Tree(a,[])::l-&gt; [Tree(a,[])]@(aux l)
                        | Tree(a,t)::[]-&gt; [Tree(a, (List.rev (aux t)))]
                        | Tree(a,t)::l-&gt; [Tree(a, (List.rev (aux t)))]@(aux l)
                    in aux l));;
  </code></pre>
  
  <p>I have tried it with this tree:</p>
  
  <pre><code>let p = Tree(1, [
      Tree(2,[]);
      Tree(3, [
          Tree(6,[]);
          Tree(7,[])
          ]);
      Tree(4,[]);
      Tree(5,[])
  ]);;
  </code></pre>
  
  <p>And I got as result of <code># spec p;;</code> </p>
  
  <pre><code>-: int tree = Tree (1, [
                  Tree (5,[]); 
                  Tree (4,[]); 
                  Tree (3,[
                     Tree(7,[]); 
                     Tree(6,[])]); 
                  Tree (2,[])
                ])
  </code></pre>
  
  <p>So I guess my function works as expected. Let me know if it is not correct</p>