题解 | #二叉树的中序遍历#

二叉树的中序遍历

https://www.nowcoder.com/practice/0bf071c135e64ee2a027783b80bf781d

//中序遍历
/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型vector
     */
     void inOrder(TreeNode* root, vector<int>& ans)
     {
        if(!root)return;
        inOrder(root->left, ans);
        ans.push_back(root->val);
        inOrder(root->right, ans);
     }
    vector<int> inorderTraversal(TreeNode* root) {
        // write code here
        vector<int> ans;
        inOrder(root, ans);
        return ans;
    }
};

全部评论

相关推荐

风的叶脉:不知道但我想要鞭打你( '-' )ノ)`-' ) 加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务