leetcode590 C++ 156ms N叉树的后序遍历

/*
// 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:
    int post(Node* root, vector<int>& res){
        if(root == nullptr){
            return 0;
        }
        for(auto child:root->children){
            post(child, res);
        }
        res.push_back(root->val);
        return 0;
    }
    
    vector<int> postorder(Node* root) {
        vector<int> res;
        if(root == nullptr){
            return res;
        }
        post(root, res);
        return res;
        
    }
};
posted @ 2018-07-30 16:45  一条图图犬  阅读(476)  评论(0编辑  收藏  举报