题解 | #求二叉树的前序遍历#
求二叉树的前序遍历
https://www.nowcoder.com/practice/501fb3ca49bb4474bf5fa87274e884b4
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型vector */ vector<int> ans; vector<int> preorderTraversal(TreeNode* root) { // write code here if(!root) return ans; ans.emplace_back(root->val); preorderTraversal(root->left); preorderTraversal(root->right); return ans; } };
C++题库 文章被收录于专栏
非淡泊无以明志,非宁静无以致远