struct TreeNode {
int val;
vector<TreeNode*> children;
TreeNode(int x) : val(x) {}
};
void preOrder(TreeNode* root) {
if (!root) return;
cout << root->val << " ";
for (auto child : root->children) {
preOrder(child);
}
}
int main() {
// 构造一棵树
TreeNode* root = new TreeNode(1);
TreeNode* node2 = new TreeNode(2);
TreeNode* node3 = new TreeNode(3);
TreeNode* node4 = new TreeNode(4);
TreeNode* node5 = new TreeNode(5);
root->children.push_back(node2);
root->children.push_back(node3);
node2->children.push_back(node4);
node3->children.push_back(node5);
// 先根遍历
preOrder(root);
return 0;
}