There is an error in the non-recursive implementation of the preorder traversal of a binary tree, but there is no problem in debugging
When implementing a preorder traversal of a binary tree using a non-recursive approach, I wrote my own stack. An error occurred during runtime.
Why using else if instead of if here when building a binary tree?
When building the insertion function in binary tree, I use two “if ” statement to track the node( if the value is smaller than the current node,go left;if bigger,go right) . I know the difference between “else if” and “if” before. So initially the result I suppose is both fine with “else if” and with “if” in this condition. I choose “if”. But eventually it turns out it only works for “else if”. When using “if” result shows “segmentation fault”.
Here is the code segment that is correct, the incorrect version changes the “elseif” to “if”.