队列,我小看了你!!!

力扣101

解题思路

平时使用vector比较多,反而将队列给生疏了。
在练习了102-二叉树的层序遍历之后,再写这道题,能够更好的明白队列的使用。
好像队列和树相结合的运用比较多。
队列的特点就是先进先出。就相当于告诉内存 hey body,我现在还不知道我要怎么处理这些数据,但是我知道某个点我会来处理的。
你要在我需要处理的时候,按照我给你的顺序给我哦!

我突然明白循环不变量是什么了。
循环不变量是因为循环结构中,需要保持处理步骤可迭代,即可循环处理。
而不单单指的是某个变量。
Am i right???

等高铁写…

代码

/**
 * 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:
    bool isSymmetric(TreeNode* root) {
        TreeNode *left, *right;
        if(!root) return true;
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        while(!q.empty()) {
            // 取对称节点
            left=q.front();
            q.pop();
            right=q.front();
            q.pop();
            // 镜面对称的节点是否一致
            if(left==NULL && right==NULL)
                continue;
            if(left==NULL || right==NULL)
                return false;
            if(left->val!=right->val)
                return false;
            // 左孩子的左孩子与右孩子的右孩子
            q.push(left->left);
            q.push(right->right);
            // 左孩子的右孩子和右孩子的左孩子
            q.push(left->right);
            q.push(right->left);
        }
        return true;
    }
};

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的无锁队列C++实现: ```cpp #include <atomic> #include <iostream> using namespace std; template <typename T> class LockFreeQueue { public: LockFreeQueue() { Node* dummy = new Node(); m_head.store(dummy); m_tail.store(dummy); } ~LockFreeQueue() { while (m_head != m_tail) { Node* next = m_head.load()->next; delete m_head.load(); m_head.store(next); } delete m_head.load(); } void push(T value) { Node* newNode = new Node(value); Node* tail; Node* next; while (true) { tail = m_tail.load(); next = tail->next.load(); if (tail == m_tail) { if (next == nullptr) { if (tail->next.compare_exchange_strong(next, newNode)) { break; } } else { m_tail.compare_exchange_strong(tail, next); } } } m_tail.compare_exchange_strong(tail, newNode); } bool pop(T& value) { Node* head; Node* tail; Node* next; while (true) { head = m_head.load(); tail = m_tail.load(); next = head->next.load(); if (head == m_head) { if (head == tail) { if (next == nullptr) { return false; } m_tail.compare_exchange_strong(tail, next); } else { if (m_head.compare_exchange_strong(head, next)) { value = next->value; delete head; break; } } } } return true; } private: struct Node { T value; atomic<Node*> next; Node() : next(nullptr) {} Node(T value) : value(value), next(nullptr) {} }; atomic<Node*> m_head; atomic<Node*> m_tail; }; int main() { LockFreeQueue<int> lfq; lfq.push(1); lfq.push(2); lfq.push(3); int value; while (lfq.pop(value)) { cout << value << endl; } return 0; } ``` 这里使用了C++11标准中的atomic库,实现了一个无锁队列队列中的每个节点都是一个包含value和next两个成员的结构体。push()函数将新节点添加到队列尾部,pop()函数从队列头部弹出节点。由于无锁队列的操作是非阻塞的,因此需要使用循环来保证队列的一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值