add: 220312-CN [cpp]
This commit is contained in:
parent
a43b370107
commit
91803365ac
|
|
@ -0,0 +1,38 @@
|
||||||
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
// Definition for a Node.
|
||||||
|
class Node {
|
||||||
|
public:
|
||||||
|
int val;
|
||||||
|
std::vector<Node*> children;
|
||||||
|
|
||||||
|
Node(int _val, std::vector<Node*> _children) : val(_val), children(std::move(_children)) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 589. N-ary Tree Preorder Traversal
|
||||||
|
* Given the root of an n-ary tree, return the preorder traversal of its nodes' values.
|
||||||
|
* Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 590. N-ary Tree Postorder Traversal
|
||||||
|
* Given the root of an n-ary tree, return the postorder traversal of its nodes' values.
|
||||||
|
* Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static std::vector<int> postorder(Node* root) {
|
||||||
|
std::vector<int> ret;
|
||||||
|
std::function<void(const Node* const)> traverse = [&](const Node* const ptr) {
|
||||||
|
if (!ptr) return;
|
||||||
|
for (const Node* const i : ptr->children)
|
||||||
|
traverse(i);
|
||||||
|
ret.push_back(ptr->val);
|
||||||
|
};
|
||||||
|
traverse(root);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2203)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2203 220313.cpp)
|
ADD_EXECUTABLE(2203 220312-CN.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue