Fork me on GitHub

Leetcode590N-ary Tree Postorder TraversalN叉树的后序遍历

给定一个 N 叉树,返回其节点值的后序遍历。

 

class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};

class Solution {
public:
    vector<int> res;
    vector<int> postorder(Node* root)
    {
        if(root == NULL)
            return res;
        Fun(root);
        return res;
    }

    void Fun(Node* root)
    {
        if(root == NULL)
            return;
        int len = root ->children.size();
        for(int i = 0; i < len; i++)
        {
            if(root ->children[i] != NULL)
                Fun(root ->children[i]);
        }
        res.push_back(root ->val);
    }
};

 

posted @ 2018-10-31 20:05  lMonster81  阅读(67)  评论(0编辑  收藏  举报
/*评论*/ /*top按钮*/

/* 网易云控件 */