leetcode 590.N-ary Tree Postorder Traversal N叉樹的後序遍歷
阿新 • • 發佈:2019-01-30
img tor nbsp code 遞歸 col image bubuko bsp
遞歸方法
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 };
leetcode 590.N-ary Tree Postorder Traversal N叉樹的後序遍歷