590. N-ary Tree Postorder Traversal

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?

 递归解法

#include<vector>
using namespace std;
// 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 {
    vector<int> res;
public:
    vector<int> postorder(Node* root) {
        res.clear();
        helper(root);
        return res;
    }
    void helper(Node* root){
        if(root==NULL) return;
        for(auto a: root->children)
            helper(a);
        res.push_back(root->val);
    }
};

 迭代解法

#include<vector>
#include<stack>
#include<iostream>
using namespace std;
// 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 {
    vector<int> res;
public:
    vector<int> postorder(Node* root) {
        res.clear();
        helper(root);
        return res;
    }

    void helper(Node* root){
        if(root==NULL)
            return;
        std::stack<pair<Node*,int>> st;
        st.push(make_pair(root,-1));
        while(!st.empty()){
            Node* parent= st.top().first;
            int child_in_st_index=st.top().second;

            while(child_in_st_index+1<=int(parent->children.size()-1)){
                //std::cout<<parent->val<<endl;
                //printf("1\n");
                parent= parent->children[child_in_st_index+1];
                st.push(make_pair(parent,-1));
                child_in_st_index=-1;
            }
            //std::cout<<"2"<<endl;
            //std::cout<<st.top().first->val<<endl;
            //std::cout<<st.top().second<<endl;
            res.push_back(st.top().first->val);
            st.pop();
            if(!st.empty()){
                pair<Node*,int> a=st.top();
                st.pop();
                a.second=a.second+1;
                st.push(a);
            }
        }
    }


    /*
    void helper(Node* root){
        if(root==NULL) return;
        for(auto a: root->children)
            helper(a);
        res.push_back(root->val);
    }
     */
};



int main(){
    Node* node5=new Node(5,{});
    Node* node6=new Node(6,{});
    Node* node3=new Node(3,{node5,node6});
    Node* node2=new Node(2,{});
    Node* node4=new Node(4,{});
    Node* node1=new Node(1,{node3,node2,node4});
    std::cout<<node5->children.size()<<std::endl;
    Solution solution;
    vector<int> res=solution.postorder(node1);
    for(auto a: res)
        std::cout<<a<<std::endl;


    return 0;
}

迭代解法整理

class Solution {
    vector<int> res;
public:
    vector<int> postorder(Node* root) {
        res.clear();
        helper(root);
        return res;
    }

    void helper(Node* root){
        if(root==NULL)
            return;
        std::stack<pair<Node*,int>> st;
        st.push(make_pair(root,-1));
        while(!st.empty()){
            Node* parent= st.top().first;
            int child_in_st_index=st.top().second;
            while(child_in_st_index+1<=int(parent->children.size()-1)){
                parent= parent->children[child_in_st_index+1];
                st.push(make_pair(parent,-1));
                child_in_st_index=-1;
            }
            res.push_back(st.top().first->val);
            st.pop();
            if(!st.empty()){
                st.top().second+=1;
            }
        }
    }
};

 

 

posted @ 2018-10-10 01:27  hopskin1  阅读(165)  评论(0编辑  收藏  举报