589 - Preorder Traversal of N-ary Trees

Original title link: force buckle


describe:

Given a root node root of an n-ary tree, return a preorder traversal of its node values.

An n-ary tree is represented serially in a level-order traversal in the input, with each set of child nodes separated by the null value (see example).


Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: [1,3,5,6,2,4]
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]
output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
 

hint:

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

Advanced: The recursive method is easy, can you use the iterative method to complete this problem?

Source: LeetCode
Link: https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal The
copyright belongs to Leetcode.com. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Problem solving ideas:

* Problem-solving ideas: 
* Recursive method: 
* It is relatively simple, first record the value of the root node, and then traverse each child node to perform the recursive operation. 
* <p> 
* Iterative method: 
* In the case of iterative method, my idea is to build two sets, respectively storing the node index and list traversed by each layer (because the child node holds a reference to the parent) 
* Traversing each layer At this time, if the current node has a subset, the level +1 (level++), and the index starts from 0. 
* If all current nodes or subsets are empty after traversing, the level is -1 (level--), and the index value of the previous level is +1;

Code:

public class Solution589 {

    //递归法
    public List<Integer> preorder(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        preorder3(root, list);
        return list;
    }

    //递归法
    public void preorder2(Node root, List<Integer> list) {
        list.add(root.val);
        for (Node node : root.children) {
            preorder2(node, list);
        }
    }

    //迭代法
    public void preorder3(Node root, List<Integer> list) {
        Map<Integer, Node> levelList = new HashMap<>();
        Map<Integer, Integer> levelIndexList = new HashMap<>();
        int level = 0;
        levelList.put(0, root);
        list.add(root.val);
        levelIndexList.put(0, 0);

        while (true) {
            Node node = levelList.get(level);//遍历的永远是最后一级节点
            List<Node> children = node.children;

            int index = levelIndexList.get(level);
            if (children.size() == 0 || index == children.size()) {
                levelList.remove(level);
                level--;
                if (level < 0) {
                    break;
                }
                levelIndexList.put(level, levelIndexList.get(level) + 1);
            } else {
                Node node1 = children.get(index);
                if (node1 == null) {
                    levelIndexList.put(level, index + 1);
                    continue;
                }
                //加入节点值
                list.add(node1.val);
                level++;
                levelList.put(level, node1);
                levelIndexList.put(level, 0);
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/123398540