版权声明:本文为博主原创文章,未经博主许可允许转载。 https://blog.csdn.net/qq_29600137/article/details/89218074
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2]
题目大意:
二叉树中序遍历。
解题思路:
按照 左 输出 右的递归即可。
/**
* 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 {
private:
vector<int> ans;
void dfs(TreeNode* x){
if(x==NULL) return;
dfs(x->left);
ans.push_back(x->val);
dfs(x->right);
}
public:
vector<int> inorderTraversal(TreeNode* root) {
dfs(root);
return ans;
}
};