LeetCode-590. N-ary Tree Postorder Traversal

网友投稿 694 2022-08-25

LeetCode-590. N-ary Tree Postorder Traversal

LeetCode-590. N-ary Tree Postorder Traversal

Given an n-ary tree, return the postorder traversal of its nodes' values.

For example, given a ​​3-ary​​ tree:

Return its postorder traversal as: ​​[5,6,3,2,4,1]​​.

Note:

Recursive solution is trivial, could you do it iteratively?

题解:

class Solution {public: static void postOrder(Node* root, vector &ans) { if (root != NULL) { for (int i = 0; i < root->children.size(); i++) { postOrder(root->children[i], ans); } ans.push_back(root->val); } } vector postorder(Node* root) { vector ans; postOrder(root, ans); return ans; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:相似图片搜索的原理(二)(搜相似图片)
下一篇:LeetCode-98. Validate Binary Search Tree
相关文章

 发表评论

暂时没有评论,来抢沙发吧~