解题思路:
前序遍历是先保存当前节点的值,然后递归遍历其子节点的值。
代码实现如下:
/*
// 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 {
private List<Integer> list=new ArrayList<Integer>();
public List<Integer> preorder(Node root) {
if(root==null){
return list;
}
list.add(root.val);
for(Node node:root.children){
preorder(node);
}
return list;
}
}
来源:CSDN
作者:我就是个渴望成长的小菜鸡
链接:https://blog.csdn.net/junjunjiao0911/article/details/104001622