The general reason is that it’s not initialized, it’s running as a random number;
Or it returns a wild pointer, which is common in test cases with empty sets and requires a pointer to be returned. Most of the time, it will be like this without attention:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
TreeNode *result;
if(preorder.size() == 0 && inorder.size() == 0)
return result;
else
return sub_bT(0, preorder.size()-1, 0, inorder.size()-1, preorder, inorder);
}
At this time, we often think that a null pointer is returned, but in fact, it is not initialized. What is returned is a wild pointer, which does not know where to point to. However, when we Run Code, no error will be reported and the result is [], so we think that a null pointer is returned.
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
TreeNode *result(NULL);
if(preorder.size() == 0 && inorder.size() == 0)
return result;
else
return sub_bT(0, preorder.size()-1, 0, inorder.size()-1, preorder, inorder);
}