LeetCode---590. N-ary Tree Postorder Traversal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | //url:https://leetcode.com/problems/n-ary-tree-postorder-traversal/description/ /* // 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 { public: vector<int> postorder(Node* root) { postorderEx(root); return _res; } void postorderEx(Node* root){ if(!root) return; //std::cout<<" v="<<root->val<<endl; for(int i=0;i<root->children.size();i++){ postorderEx(root->children[i]); } _res.push_back(root->val); } vector<int> _res; }; |
原创文章,转载请注明: 转载自royalchen的博客
发表评论