LeetCode 589. N-ary Tree Preorder Traversal

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 1:

LeetCode 589. N-ary Tree Preorder Traversal_第1张图片

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

Example 2:

LeetCode 589. N-ary Tree Preorder Traversal_第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]
Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]

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.

Follow up: Recursive solution is trivial, could you do it iteratively?


多叉树的preorder traversal,跟二叉树几乎一样吧,没什么差别。

递归很好写,加完root以后,把root的所有children遍历一遍就好了。

/*
// Definition for a Node.
class Node {
    public int val;
    public List children;

    public Node() {}

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

    public Node(int _val, List _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List preorder(Node root) {
        List result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        helper(root, result);
        return result;
    }
    
    private void helper(Node root, List result) {
        result.add(root.val);
        if (root.children.size() == 0) {
            return;
        }
        for (Node node : root.children) {
            helper(node, result);
        }
    }
}

然后发现其实可以优化一下:

/*
// Definition for a Node.
class Node {
    public int val;
    public List children;

    public Node() {}

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

    public Node(int _val, List _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List preorder(Node root) {
        List result = new ArrayList<>();
        helper(root, result);
        return result;
    }
    
    public void helper(Node root, List result) {
        if (root == null) {
            return;
        }
        result.add(root.val);
        for (int i = 0; i < root.children.size(); i++) {
            helper(root.children.get(i), result);
        }
    }
}

 


迭代也挺好写的,也是一样的套路,用stack存,然后往里push children的时候从右往左push。

/*
// Definition for a Node.
class Node {
    public int val;
    public List children;

    public Node() {}

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

    public Node(int _val, List _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List preorder(Node root) {
        List result = new ArrayList<>();
        Deque stack = new ArrayDeque<>();
        if (root != null) {
            stack.push(root);
        }
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            result.add(node.val);
            int size = node.children.size();
            for (int i = 0; i < size; i++) {
                stack.push(node.children.get(size - 1 - i));
            }
        }
        return result;
    }
}

你可能感兴趣的:(LeetCode,leetcode)