> For the complete documentation index, see [llms.txt](https://code-snippets.hbamithkumara.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-snippets.hbamithkumara.com/leetcode/problems/501-600/n-ary-tree-preorder-traversal.md).

# 589. N-ary Tree Preorder Traversal

### Description

Given the `root` of an n-ary tree, return *the preorder traversal of its nodes' values*.

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

### Constraints

* The number of nodes in the tree is in the range `[0, 104]`.
* `0 <= Node.val <= 104`
* The height of the n-ary tree is less than or equal to `1000`.

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)
* ProgramCreek
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:** root = \[1, null, 3, 2, 4, null, 5, 6]

![](/files/-MYk_5hTXSlFJh_S9oGb)

**Output:** \[1, 3, 5, 6, 2, 4]
{% endtab %}

{% tab title="Example 2" %}
**Input:** root = \[1, null, 2, 3, 4, 5, null, null, 6, 7, null, 8, null, 9, 10, null, null, 11, null, 12, null, 13, null, null, 14]

<div align="left"><img src="/files/-MYk_f2I1Xr53W2bYDSK" alt=""></div>

**Output:** \[1, 2, 3, 6, 7, 11, 14, 4, 8, 12, 5, 9, 13, 10]
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Node" %}

```java
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
```

{% endtab %}

{% tab title="Solution 1" %}

```java
/**
 * Time complexity : O(N)
 * Space complexity : O(N)
 */

class Solution {
    public List<Integer> preorder(Node root) {
        List<Integer> resultList = new LinkedList();
        if(root == null) {
            return resultList;
        }
        preorder(root, resultList);
        return resultList;
    }
    
    private void preorder(Node node, List<Integer> resultList) {
        if(node == null) {
            return;
        }
        resultList.add(node.val);
        if(node.children == null) {
            return;
        }
        for(Node child: node.children) {
            preorder(child, resultList);
        }
    }
}
```

{% endtab %}

{% tab title="Solution 2" %}

```java
/**
 * Time complexity : O(N)
 * Space complexity : O(N)
 */
 
 class Solution {
  public List<Integer> preorder(Node root) {
    LinkedList<Node> stack = new LinkedList<>();
    LinkedList<Integer> output = new LinkedList<>();
    if (root == null) {
      return output;
    }

    stack.add(root);
    while (!stack.isEmpty()) {
      Node node = stack.pollLast();
      output.add(node.val);
      Collections.reverse(node.children);
      for (Node item : node.children) {
        stack.add(item);
      }
    }
    return output;
  }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*
