Leetcode 94. 二叉树的中序遍历(DAY 7)(DAY 9 迭代 递归 Morris)

文章目录

    • 原题题目
    • 代码实现(首刷自解)
    • 代码实现(二刷Morris)
    • 代码实现(迭代)
    • 代码实现(三刷DAY 85 递归)
    • 代码实现(四刷DAY 134 递归)
    • 代码实现(四刷C++ DAY134 迭代)


原题题目

Leetcode 94. 二叉树的中序遍历(DAY 7)(DAY 9 迭代 递归 Morris)_第1张图片




代码实现(首刷自解)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
#define MAX 101


void visit(struct TreeNode* root,int* returnSize,int* arr)
{
     
    if(root)
    {
     
        visit(root->left,returnSize,arr);
        arr[(*returnSize)++] = root->val;
        visit(root->right,returnSize,arr);
    }
}

int* inorderTraversal(struct TreeNode* root, int* returnSize){
     
     int* arr = (int*)malloc(sizeof(int) * MAX);
     (*returnSize) = 0;
     visit(root,returnSize,arr);
     return arr;
}


代码实现(二刷Morris)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
#define MAX 101

int* inorderTraversal(struct TreeNode* root, int* returnSize){
     
     int* arr = (int*)malloc(sizeof(int) * MAX);
     (*returnSize) = 0;
    struct TreeNode* position;
    while(root)
    {
     
        position = root->left;
        if(!position)
        {
     
            arr[(*returnSize)++] = root->val;
            root = root->right;
        }
        else
        {
     
            while(position->right != root && position->right)
                position = position->right;
            if(!position->right)
            {
     
                position->right = root;
                root = root->left;
            }
            else
            {
     
                position->right = NULL;
                arr[(*returnSize)++] = root->val;
                root = root->right;
            }
        }
    } 
    return arr;
}


代码实现(迭代)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
#define MAX 101

int* inorderTraversal(struct TreeNode* root, int* returnSize){
     
    int* arr = (int*)malloc(sizeof(int) * MAX),size = 0;
    (*returnSize) = 0;
    struct TreeNode* position = root,* stack[MAX];
    while(position != NULL || size > 0)
    {
     
        while(position != NULL)
        {
     
            stack[size++] = position;            
            position = position->left;
        }
        position = stack[--size];
        arr[(*returnSize)++] = position->val;        
        position = position->right;
    }
    return arr;
}

代码实现(三刷DAY 85 递归)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
     
public:
    vector<int> ret;

    void visit(TreeNode* root)
    {
     
        if(!root)   return;
        visit(root->left);
        ret.push_back(root->val);
        visit(root->right);
        return;
    }
    vector<int> inorderTraversal(TreeNode* root) {
     
        visit(root);
        return ret;
    }
};

代码实现(四刷DAY 134 递归)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
     
public:
    TreeNode* visit(const vector<int>& nums,int left,int right)
    {
     
        if(left > right)    return nullptr;
        int pos = (left+right)/2;
        auto root = new TreeNode(nums[pos],visit(nums,left,pos-1),visit(nums,pos+1,right));
        return root;
    }

    TreeNode* sortedArrayToBST(vector<int>& nums) {
     
        return visit(nums,0,nums.size()-1);
    }
};

代码实现(四刷C++ DAY134 迭代)


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
     
public:
    vector<int> inorderTraversal(TreeNode* root) {
     
        struct TreeNode* pos = root;
        stack<TreeNode*> stack;
        vector<int> ret;
        while(stack.size() || pos)
        {
     
            while(pos)
            {
     
                stack.emplace(pos);
                pos = pos->left;
            }
            if(stack.size())
            {
     
                ret.emplace_back(stack.top()->val);
                pos = stack.top()->right;
                stack.pop();
            }
        }
        return ret;
    }
};

你可能感兴趣的:(#,从c开始的进步之路,c语言)