【Leetcode】94.二叉树的中序遍历C++(递归法)

在这里插入图片描述在这里插入图片描述

#include "iostream"

#include "vector"

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution
{
public:
    vector<int> ans;
    vector<int> inorderTraversal(TreeNode *root)
    {
        if(root==NULL)
        {
            return ans;
        }

        if (root->left)
        {
            inorderTraversal(root->left);
        }

        ans.push_back(root->val);

        if (root->right)
        {
            inorderTraversal(root->right);
        }

        return ans;
    }
};
发布了103 篇原创文章 · 获赞 128 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44936889/article/details/104100473