leetCode.94.94. 二叉树的中序遍历
没什么好讲思路的,直接上代码
/**
* 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> res;
vector<int> inorderTraversal(TreeNode* root) {
dfs( root );
return res;
}
void dfs( TreeNode * root ) {
if ( !root ) return ;
dfs( root->left );
res.push_back(root->val);
dfs( root->right );
}
};