Geek

博客园 首页 新随笔 联系 订阅 管理

 

 

/*
// 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> iList = new LinkedList<>();
        dfs(iList,root);
        return iList;
    }
    void dfs(List<Integer> res,Node cur) {
        if(cur == null) return;
        res.add(cur.val);
        cur.children.forEach(c -> {
            dfs(res,c);
        });
    }
}

 

posted on 2020-07-15 19:47  .geek  阅读(207)  评论(0编辑  收藏  举报