树的先序遍历,使用递归实现。
class Solution { public: vector<Node> Tree; void preTree(Node node) { Tree.push_back(Node(node.val, node.children)); for (auto n : node.children) { Node node; node = Node(n->val, n->children); preTree(node); } } vector<int> preorder(Node* root) { vector<int> V; if (root != NULL) { preTree(*root); for (auto n : Tree) { V.push_back(n.val); } } return V; } };