(N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal

Posted weixu-liu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal相关的知识,希望对你有一定的参考价值。

Given an n-ary tree, return the postorder traversal of its nodes‘ values.

For example, given a 3-ary tree:

 

技术图片

 

Return its postorder traversal as: [5,6,3,2,4,1].

 

Note:

Recursive solution is trivial, could you do it iteratively?

---------------------------------------------------------------------------------------------------------------------------------

leetcode589. N-ary Tree Preorder Traversal类似。用递归会简单些

C++代码:

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<int> postorder(Node* root) {
        vector<int> vec;
        helper(root,vec);
        return vec;
    }
    void helper(Node* root,vector<int> &vec){
        if(!root) return;
        for(Node *cur:root->children){
            helper(cur,vec);
        }
        vec.push_back(root->val);
    }
};

 

以上是关于(N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Algorithm 590. N 叉树的后序遍历

LeetCode Algorithm 590. N 叉树的后序遍历

Leetcode刷题100天—590.N叉树的后序遍历(二叉树)—day12

leetcode 590.N-ary Tree Postorder Traversal N叉树的后序遍历

LeetCode 589. N 叉树的前序遍历(迭代写法) / 2049. 统计最高分的节点数目 / 590. N 叉树的后序遍历

LeetCode 590 N叉树的后序遍历[dfs 树] HERODING的LeetCode之路