N 叉树的后序遍历 (N-ary Tree Postorder Traversal)

 

思路:

// @Title: N 叉树的后序遍历 (N-ary Tree Postorder Traversal)
// @Author: qisiii
// @Date: 2022-03-12 10:41:25
// @Runtime: 1 ms
// @Memory: 41.8 MB
// @comment: 
// @flag: 
/*
// 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 {
    public List<Integer> postorder(Node root) {
        if(root==null){
            return new ArrayList();
        }
        List<Integer> result=new ArrayList();
        if(!root.children.isEmpty()){
            for(Node child:root.children){
                result.addAll(postorder(child));
            }
        }
        result.add(root.val);
        return result;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18