【发布时间】:2021-11-27 06:00:14
【问题描述】:
问题链接是这样的:Inorder Traversal (GFG) 我参考了 geeksforgeeks 文章,该文章具有相同的代码但具有 void 函数。我对其进行了修改以适应问题。现在我遇到了分段错误,我不知道为什么。 GFG 文章:Inorder Tree Traversal without recursion and without stack!
vector<int> inOrder(Node* root) {
// Your code here
vector<int>ans;
Node* current = root;
Node* pre;
if(!root){return ans;}
while(current){
if(!root->left){
ans.push_back(current->data);
current = current->right;
}
else{
pre = current->left;
while(pre->right && (pre->right != current)){pre = pre->right;}
if(!pre->right){
pre->right = current;
current = current->left;
}
else{
pre->right = NULL;
ans.push_back(current->data);
current = current->right;
}
}
}
return ans;
}
【问题讨论】:
-
首先欢迎来到 Stack Overflow。请阅读the help pages,接受SO tour,阅读How to Ask,以及this question checklist。最后请学习如何创建一个minimal reproducible example,以及如何edit你的问题来改进它。
-
至于你的问题,你的调试器在你用它来捕捉崩溃的时候会告诉你什么?它发生在您的代码中的哪个位置?在崩溃的时间和点,所有变量的值是多少?您是否尝试在监视变量的同时在调试器中单步执行代码,同时在对树执行操作时绘制和重绘树?
-
@Someprogrammerdude 查找“莫里斯遍历”
-
您的代码没有复制正确,至少有一处错别字。
标签: c++ binary-tree inorder