206、N叉树的前续遍历
题目描述:
很简单的一道题目,没有什么技巧,直接递归
代码:
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List<Integer> preorder(Node root) {
List<Integer> result = new ArrayList<>();
pre(root, result);
return result;
}
public static void pre(Node root,List<Integer> tem){
if(root == null){
return ;
}
tem.add(root.val);
List<Node> nodes = root.children;
for (Node node : nodes) {
pre(node, tem);
}
}
}