589. N叉树的前序遍历

/*
// 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;
    }
};
*/

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

 

posted @ 2021-01-15 14:19  青衫客36  阅读(67)  评论(0编辑  收藏  举报