Skip to content

Latest commit

 

History

History
29 lines (29 loc) · 785 Bytes

DAY6P1.md

File metadata and controls

29 lines (29 loc) · 785 Bytes

N-ary Tree Preorder Traversal


  • Question:

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)


  • Example:

alt text

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

Output: [1,3,5,6,2,4]


  • Solution:

Code :

class Solution {
    ArrayList<Integer> ans=new ArrayList<>();
    public List<Integer> preorder(Node root) {
        if(root==null)
            return ans;
        ans.add(root.val);
        for(Node node: root.children)
        {
            preorder(node);
        }
        return ans;
    }
}