leetcode---94. Binary Tree Inorder Traversal C++题解

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);
    }
};

猜你喜欢

转载自blog.csdn.net/Morzker/article/details/88398483
今日推荐