stl - 未对齐地址内的成员访问
问题描述
问题链接:https ://leetcode.com/problems/cousins-in-binary-tree
我的代码:
/**
* 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 isCousins(TreeNode* root, int x, int y) {
//breath first search will be better in this approach
queue< pair< TreeNode * , TreeNode *> > a;
queue< pair< TreeNode * , TreeNode *> > b; // first children..then parent
a.push({root,NULL});//root-> parent = NULL
bool check = false;
auto it = a.front();
auto it2 = a.front();
while(a.size() !=0 || b.size() !=0){
if(a.size() == 0 && b.size() != 0)
a.swap(b);
//if(b.size() == 0)
// b.push({NULL,NULL});
it = a.front();
if(it.first->left != NULL)
b.push({it.first->left,it.first});
if(it.first->right != NULL)
b.push({it.first->right,it.first});
if( it.first != NULL && (it.first->val == x || it.first->val == y)){
//we need to find both in this level
//if not found return false;
while(a.size() != 0){
a.pop();
it2 = a.front();
//this is the line 40
//here....
if(it2.first != NULL && (((it2.first)->val == x) || ((it2.first)->val == y))){//all nodes are unique in value
if(it2.second == it.second)
return false;//same parent ..so not cousin
return true;//cousim
}
}
//only one of them is found at present depth
return false;
}
if(a.size() !=0)
a.pop();
}
//both not found
return false;
}
};
运行时错误:第 40 行:字符 60:运行时错误:在未对齐的地址 0xbebebebebebebebe 内访问类型“TreeNode”的成员,这需要 8 字节对齐 (solution.cpp) 0xbebebebebebebebe:注意:指针指向此处摘要:UndefinedBehaviorSanitizer:未定义行为 prog_joined。 cpp:49:60
解决方案
推荐阅读
- postgresql - 我应该如何在 Postgres 中存储 Go 的 time.Location?
- sql - 如何检查重复的 CLOB - Oracle?
- python - 旋转框小部件内的变量,可在单击按钮小部件时显示在 LCD 小部件中
- reactjs - 如何计算渲染函数中的行数?
- c# - 如何在 GetEnumerator 中返回动态实体
- sql - 在 SQL Developer 的脚本中传递和使用参数
- bootstrap-4 - 如何在 Bootstrap ul li 上设置半径?
- oracle - 是否可以从 Oracle 11g 标准版转储文件将 schma 导入 Oracle 18c 快捷版?
- ios - Apple-app-site-association 文件未链接到应用程序
- python - 在 python 中实现“Eratosthenes 筛”时出现问题的过滤器行为