代码随想录:二叉树的递归遍历
代码随想录:二叉树的递归遍历
现在是找借口时间,一开始是期末考试太忙了,后来是过年放假,一晃这么久没写题了,这样不好。,看了一下我现在leetcode才40多道题呢定个目标,三月之前刷完代码随想录,并且把hot100的简单中等题都写了。
/**
* 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:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
traver(root, res);
return res;
}
void traver(TreeNode* root, vector<int>& res) {
if (root == nullptr) {
return;
} else {
traver(root->left, res);
res.push_back(root->val);
traver(root->right, res);
}
}
};

浙公网安备 33010602011771号