LeetCode590. N叉树的后序遍历

题目

 1 class Solution {
 2 public:
 3     vector<int>ans;
 4     vector<int> postorder(Node* root) {
 5         dfs(root);
 6         return ans;
 7     }
 8     void dfs(Node* root){
 9         if(root == NULL) return;
10         for(int i = 0;i < root->children.size();i++){
11             dfs(root->children[i]);
12         }
13         ans.push_back(root->val);
14     }
15 };
 1 class Solution {
 2 public:
 3     
 4     vector<int> postorder(Node* root) {
 5         vector<int>ans;
 6         stack<Node*>s;
 7         s.push(root);
 8         while(!s.empty()){
 9             Node* node = s.top();s.pop();
10             if(node) ans.push_back(node->val);
11             else continue;
12 
13             for(int i = 0;i<node->children.size();i++)
14                 s.push(node->children[i]);
15         }
16         reverse(ans.begin(),ans.end());return ans;
17     }
18     
19 };
原文地址:https://www.cnblogs.com/fresh-coder/p/14259026.html