I'm doing a depth first search on a binary tree. My function always returns false no matter what I do. I step through the code with break points and it does find the number I'm looking for and return true, ending the function, but then it decides to jump to the last "else" statement and run from there, changing it to false. Why does it keep jump backwards after it returns true?
You need to pass true all the way back to the original caller. As an example, say that the node that you are searching for is one level below and less than the root node. You call dfs() and then call dfs() again passing a pointer to the left node. That call returns true to the calling dfs() and it has to complete executing so it returns false.
The solution is to return the function calls: return dfs(node->mLeft,searchKey);
and: return dfs(node->mRight,searchKey);