节点: 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 { List<Integer> res = new ArrayList<Integer>(); public List<Integer> preorder(Node root) { if(root == null) return res; res.add(root.val); for(Node child:root.children) { preorder(child); } return res; } } 非递归: class Solution { public List<Integer> preorder(Node root) { //非递归版 List<Integer> res = new ArrayList<Integer>(); Stack<Node> stack = new Stack<Node>(); if(root == null) return res; stack.push(root); while(!stack.isEmpty()) { Node node = stack.pop(); res.add (node.val); for(int i = node.children.size()-1;i>= 0;i--) { stack.add(node.children.get(i)); } } return res; } }