title: Recursively get parent nodes from specific node
body: <p>I need to make a function that returns all parent nodes from a specific node in a tree structure.</p>
  
  <p>This is an example of the structure:</p>
  
  <pre><code>struct Node
  {
      var name: String
      var id: Int
      var parentId: Int?
      var children: [Node]
  
      init(name: String, id: Int, children: [Node], parentId: Int?)
      {
          self.name = name
          self.id = id
          self.children = children
          self.parentId = parentId
      }
  }
  </code></pre>
  
  <p>This would be an array of Node.</p>
  
  <pre><code>Array(
      Node("A1", 1, [
          Node ("A11", 11, [
              Node("A111", 111, []),
              Node("A112", 112, [])
              ]
          )]),
      Node("A2", 2, [
          Node ("A21", 21, [
              Node("A211", 211, []),
              Node("A212", 212, [])
              ]
          )]),        
      )
  )
  </code></pre>
  
  <p>If I select Note "112" I should get an array like this:</p>
  
  <pre><code>[Node("A1", 1, [...]), Node ("A11", 11, [...]]
  </code></pre>
  
  <p>Any ideas how I can do this?</p>
  
  <p>Here's a testable example:</p>
  
  <pre><code>let nodeA111 = Node(name: "A111", id: 111, children: [], parentId: 11)
  let nodeA112 = Node(name: "A112", id: 112, children: [], parentId: 11)
  let nodeA211 = Node(name: "A211", id: 211, children: [], parentId: 21)
  let nodeA212 = Node(name: "A212", id: 212, children: [], parentId: 21)
  
  let nodeA11 = Node(name: "A11", id: 11, children: [nodeA111, nodeA112], parentId: 1)
  let nodeA21 = Node(name: "A21", id: 21, children: [nodeA211, nodeA212], parentId: 2)
  
  let nodeA1 = Node(name: "A1", id: 1, children: [nodeA11], parentId: nil)
  let nodeA2 = Node(name: "A2", id: 2, children: [nodeA21], parentId: nil)
  
  let nodes: [Node] = [nodeA1, nodeA2]
  
  // This is the function I want to do
  func getParentNodes(forNode node: Node) -&gt; [Node]
  {
      return []
  }
  </code></pre>
  
  <p>I appreciate the help.</p>