题解 | #牛群的轴对称结构#
牛群的轴对称结构
https://www.nowcoder.com/practice/a200535760fb4da3a4568c03c1563689
1.万能的队列
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
bool check(pair<TreeNode*, TreeNode*> st){
queue<pair<TreeNode*, TreeNode*> > q;
q.push(st);
while(!q.empty()){
TreeNode* t1 = q.front().first;
TreeNode* t2 = q.front().second;
q.pop();
if(!t1&&!t2) continue;
if(!t1||!t2||(t1->val!=t2->val)) return false;
q.push(make_pair(t1->left,t2->right));
q.push(make_pair(t1->right,t2->left));
}
return true;
}
bool isSymmetric(TreeNode* root) {
// write code here
if(!root) return true;
return check(make_pair(root,root));
}
};
