/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: void search(TreeNode* node, vector<int>& result){ if(!node) return; result.push_back(node->val); search(node->left, result); search(node->right, result); } vector<int> preorderTraversal(TreeNode* root) { vector<int> result; search(root, result); return result; } };
标签:node,right,TreeNode,val,part01,day14,二叉树,left,result From: https://www.cnblogs.com/zqh2023/p/17648787.html