589.N叉树的前序遍历
class Solution {
public List<Integer> preorder(Node root) {
List<Integer> res=new ArrayList<>();
helper(root,res);
return res;
}
public void helper(Node root,List<Integer> res){
if(root==null){
return;
}
res.add(root.val);
for(Node ch:root.children){
helper(ch,res);
}
}
}
590.N叉树的后续遍历
class Solution {
public List<Integer> postorder(Node root) {
List<Integer> res=new ArrayList<>();
helper(root,res);
return res;
}
public void helper(Node root,List<Integer> res){
if(root==null){
return;
}
for(Node ch:root.children){
helper(ch,res);
}
res.add(root.val);
}
}