其他分享
首页 > 其他分享> > leetcode 590.N-ary Tree Postorder Traversal N叉树的后序遍历

leetcode 590.N-ary Tree Postorder Traversal N叉树的后序遍历

作者:互联网

递归方法

C++代码:

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4 public:
 5     int val;
 6     vector<Node*> children;
 7 
 8     Node() {}
 9 
10     Node(int _val, vector<Node*> _children) {
11         val = _val;
12         children = _children;
13     }
14 };
15 */
16 class Solution {
17 public:
18     vector<int> postorder(Node* root) {
19         vector<int>res;
20         post(root,res);
21         return res;
22     }
23     void post(Node*root, vector<int> &res){
24         if(root==NULL) return;
25         for(int i=0;i<root->children.size();i++){
26             post(root->children[i],res);
27         }
28         res.push_back(root->val);
29     }
30 };

 

标签:Node,590,val,ary,Tree,vector,res,root,children
来源: https://www.cnblogs.com/joelwang/p/10336348.html