给定一个 N 叉树,返回其节点值的后序遍历。
例如,给定一个 3叉树
:
思路:先遍历所有孩子,再放入自己。
/*
// 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 {List<Integer> res=new ArrayList<Integer>();public List<Integer> postorder(Node root) {helper(root);return res;}public void helper(Node root){if (root==null) return;for (int i = 0; i <root.children.size() ; i++) {helper(root.children.get(i));}res.add(root.val);}
}