zoukankan      html  css  js  c++  java
  • 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
    ");
                    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;
                }
            }
        }
    };
  • 相关阅读:
    常用Linux 服务器命令--各种性能指标命令
    jenkins离线插件安装--笨方法
    Jmeter压力测试简单教程(包括服务器状态监控)
    【JMeter】JMeter使用plugins插件进行服务器性能监控
    Linux查看服务器配置常用
    Linux下内存查看命令
    【linux命令】lscpu、etc/cpuinfo详解
    Jmeter用于接口测试中【接口耦合关联的实现】
    jmeter教程--简单的做压力测试
    将字母拆分
  • 原文地址:https://www.cnblogs.com/learning-c/p/9764104.html
Copyright © 2011-2022 走看看