递归方法
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 };
原文地址:https://www.cnblogs.com/joelwang/p/10336348.html
时间: 2024-10-01 06:19:58