#树#遍历#N叉树的前序遍历
Posted lyr-2000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#树#遍历#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> 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); }); } }
以上是关于#树#遍历#N叉树的前序遍历的主要内容,如果未能解决你的问题,请参考以下文章