LeetCode94二叉树的中序遍历

法一:递归

/**
 * 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>ans;
        helper(root, ans);
        return ans;
    }
    void helper(TreeNode* &root, vector<int>&ans){
        if(NULL == root)
            return;
        helper(root->left, ans);
        ans.push_back(root->val);
        helper(root->right, ans);
    }
};

在这里插入图片描述

法二:迭代

你可能感兴趣的:(code)