CPP 程序中未定义的行为
undefined behaviour in cpp program
我是 cpp 的新手,我知道我的问题很愚蠢,但除了这里我别无他法,if 块有什么问题,我的逻辑非常正确,但我不知道为什么我的程序因为 if 块而给出 TLE
在线裁判链接 leetcode
当我更改 if 循环时
if((st.empty()==false)&&(st.top()==root)){
root=root->right;
}else{
ans.push_back(root->val);
root=NULL;
}
从下面的程序中挑选,然后运行良好,请帮助我 我是cpp的新手,我之前用python编程请帮忙。 我的 if 块有什么问题
这是我的解决方案正在获得TLE
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*>st;
vector<int>ans;
while(root||!st.empty()){
while(root){
st.push(root);
st.push(root);
root=root->left;
}
if(st.empty())return ans;
root = st.top();
st.pop();
if((st.empty()==false)&&(st.top()==root)){ //if((st.empty()==false)&&(st.top()==root))
root==root->right;
}else{
ans.push_back(root->val);
root=NULL;
}
}
return ans;
}
};
接受以下解决方案
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*>st;
vector<int>ans;
while(root||!st.empty()){
while(root){
st.push(root);
st.push(root);
root=root->left;
}
if(st.empty())return ans;
root = st.top();
st.pop();
if((st.empty()==false)&&(st.top()==root)){
root=root->right;
}else{
ans.push_back(root->val);
root=NULL;
}
}
return ans;
}
};
这是错误的:
if((st.empty()==false)&&(st.top()==root)){ //if((st.empty()==false)&&(st.top()==root))
root==root->right;
你不是在分配root
,而是在比较它。
修复:
root=root->right;
相关文章:
- 具有外部"c"和程序集的未定义函数
- CPP 程序中未定义的行为
- 在 C++17 中,是否未定义使用无锁原子学保护从信号处理程序传递的数据?
- 我无法让我的程序工作,我一直得到未定义的符号:C
- 在Linux/Ubuntu中编译openCV应用程序时,如何解决未定义的引用cv:..?
- C++CTRL+C处理程序有未定义的行为
- 在Qt应用程序中包含Python.h会导致对Qt函数的未定义引用
- 编译简单的多文件类程序时出现未定义的引用错误
- 我对C++程序有一个未定义的参考,不知道如何处理它
- 错误:尝试在全新系统安装中构建应用程序后未定义对'...'的引用
- 在 C++ 中调用时对内联程序集代码的未定义引用
- Ebpf:帮助程序的未定义符号
- C++程序调用 C 函数 在 g++ 链接期间获取未定义的引用
- r-工作程序中对自定义函数的未定义引用(C++和RcppParallel)
- MSVC 2015-我程序中的SSE编译器错误或错误/未定义的行为
- 在 qt 控制台应用程序中未定义对 'vtable for myObj' 的引用 - 信号和插槽
- 部署后的Linux qt应用程序-符号查找错误:libQt5Core.so.5未定义的符号:uncv_getDefaul
- 构建应用程序时未定义的引用错误;
- 无法将 Linux openGL 程序与 mingw32 交叉编译。配置.log错误:未定义对"_glEnable"的引用
- 架构的未定义符号x86_64惠姆使用G 编译C 程序