题解 | #二叉树的前序遍历#
二叉树的前序遍历
https://www.nowcoder.com/practice/5e2135f4d2b14eb8a5b06fab4c938635
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: void preorder(vector<int> &res, TreeNode * root) { if(root == NULL) return; res.push_back(root->val); preorder(res, root->left); preorder(res, root->right); } vector<int> preorderTraversal(TreeNode* root) { vector<int> res; preorder(res,root); return res; } };