​​​​​​144. 二叉树的前序遍历+145. 二叉树的后序遍历+94. 二叉树的中序遍历

文章讨论了C++标准库中的map、set、multimap和multiset的底层实现,这些容器基于平衡二叉搜索树,因此插入和删除操作的时间复杂度为O(logn)。相比之下,unordered_map和unordered_set使用哈希表,提供更快的平均查找速度。此外,文章提供了二叉树前序、后序和中序遍历的递归和迭代实现示例。
摘要由CSDN通过智能技术生成

C++中map、set、multimap,multiset的底层实现都是平衡二叉搜索树,所以map、set的增删操作时间时间复杂度是logn,注意我这里没有说unordered_map、unordered_set,unordered_map、unordered_set底层实现是哈希表。

这句话来自代码随想录,没看懂,希望二刷能搞明白。

​​​​​​144. 二叉树的前序遍历

递归 

/**
 * 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:

    void mpreturn(TreeNode* root , vector<int>& res )
    {
        if(root == NULL)
        {
            return;
        }
        res.push_back(root->val);
        mpreturn(root->left , res);
        mpreturn(root-> right, res);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector <int> res ;
        mpreturn (root , res);
        return res;
    }
};

把握好递归结束条件

迭代 

/**
 * 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> preorderTraversal(TreeNode* root) {
        vector <int> res ;
        stack <TreeNode*> temp;
        temp.push(root);
        if(root == NULL)
        {
            return res;
        }
        while(!temp.empty())
        {
            root = temp.top();
            res.push_back(root -> val);
            temp.pop();
            if(root -> right != NULL) temp.push(root -> right);
            if(root -> left != NULL) temp.push(root -> left);
        }
        return res;
    }
};

+

递归 

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

迭代

/**
 * 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> postorderTraversal(TreeNode* root) {
        vector <int> res ;
        stack <TreeNode*> temp;
        temp.push(root);
        if(root == NULL)
        {
            return res;
        }
        while(!temp.empty())
        {
            root = temp.top();
            res.push_back(root -> val);
            temp.pop();
            if(root -> left != NULL) temp.push(root -> left);
            if(root -> right != NULL) temp.push(root -> right);  
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

 

94. 二叉树的中序遍历 

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

    }
};

递归

/**
 * 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) {
        vector<int> res;
        stack <TreeNode*> temp;
        while(!temp.empty() || root != NULL)
        {
            if(root != NULL)
            {
                temp.push(root);
                root = root -> left;
            }
            else
            {
                root = temp.top();
                res.push_back(root -> val);
                temp.pop();
                root = root -> right;
            }
        }
        return res;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值