C++,二叉树的中序遍历,这个没什么好说的2333。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> in;
dfs(root, in);
return in;
}
void dfs(TreeNode *root, vector<int> &in) {
if (!root) return ;
dfs(root->left, in);
in.push_back(root->val);
dfs(root->right, in);
}
};