N-ary Tree Preorder Traversal

Given 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). 

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: [1,3,5,6,2,4]


Approach

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NAryTreePreorderTra {
    static List<Integerlist = null;

    public static void main(String[] args) {

        // 3 node child
        List<Nodenode3Child = new ArrayList<Node>();
        node3Child.add(new Node(5));
        node3Child.add(new Node(6));

        // 1 node child
        List<Nodenode1Child = new ArrayList<Node>();
        node1Child.add(new Node(3, node3Child));
        node1Child.add(new Node(2));
        node1Child.add(new Node(4));

        // root node
        Node node = new Node(1, node1Child);

        list = new ArrayList<>();
        preOrder(node);
        System.out.println(Arrays.asList(list));

    }

    // method to pre-order
    public static void preOrder(Node root) {
        if (root == null)
            return;

        list.add(root.val);
        List<Nodechild = root.children;
        if (child != null) {
            for (Node c : child) {
                preOrder(c);
            }
        }

    }
}

class Node {
    public int val;
    public List<Nodechildren;

    public Node() {
    }

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

    public Node(int _valList<Node_children) {
        val = _val;
        children = _children;
    }
};

C++

#include <bits/stdc++.h>
using namespace std;

//structure of the node
struct Node 
{
    int val;
    vector<Node*> children;

     Node() {
    }

    Node(int _val) {
        val = _val;
    }

    Node(int _valvector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
 //function for pre-order traversal
vector<intpreOrder(Node* root
{
        vector<intres;
        if(root==NULL)
              return res;
        stack<Node*> st;
        st.push(root);
        while(!st.empty())
        {
        Node *curr=st.top();
               st.pop();
            res.push_back(curr->val);
            for(auto it=curr->children.rbegin();it!=curr->children.rend();it++)
                  st.push(*it);     
         
        }
        return res;
}

int main()
{
        // 3 node child
        vector<Node*> node3Child;
        node3Child.push_back(new Node(5));
        node3Child.push_back(new Node(6));

        // 1 node child
        vector<Node*> node1Child;
        node1Child.push_back(new Node(3node3Child));
        node1Child.push_back(new Node(2));
        node1Child.push_back(new Node(4));

        // root node
        Node *node = new Node(1node1Child);

        vector<intresult=preOrder(node);
       for(int i=0;i<result.size();i++)
          cout<<result[i]<<" ";
       return 0;
        
}


No comments:

Post a Comment