mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
32 lines
765 B
C++
32 lines
765 B
C++
#include <vector>
|
|
|
|
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 {
|
|
private:
|
|
void traverse(std::vector<int> &list, TreeNode *root) {
|
|
if (root == nullptr) {
|
|
return;
|
|
} else {
|
|
traverse(list, root->left);
|
|
list.push_back(root->val);
|
|
traverse(list, root->right);
|
|
}
|
|
}
|
|
|
|
public:
|
|
std::vector<int> inorderTraversal(TreeNode *root) {
|
|
std::vector<int> list;
|
|
traverse(list, root);
|
|
return list;
|
|
}
|
|
};
|