2 2
/ \ / \
/ \ / \
/ \ / \
1 3 3 1
/ \ / \ / \ / \
0 7 9 1 reverse >>> 1 9 7 0
/ / \ / \ / \ / \ \
2 1 0 8 8 8 8 0 1 2
/ \
7 7
— https://leetcode.com/problems/invert-binary-tree/description...I guess I did not spell it out in my original post, but I always felt it might have been an intentional nonsense question intended to gauge how he would react to someone talking nonsense, or something like that, and not necessarily related to technical things. A sibling post of my original to suggest this was the case, without the weird detour through the term "invert binary tree".
Edit: Of course an alternative explanation is that the interview used another term and he then used the term "invert" on twitter.
Idk, this example at least has to me always felt like people who complain about not passing the driver's test coz they did some minor error and then forgetting to mention that they drove past a stop sign.
TreeNode* invertTree(TreeNode* root) {
if (root == NULL) {
return NULL;
}
TreeNode* tmp = invertTree(root->left);
root->left = invertTree(root->right);
root->right = tmp;
return root;
}
It sounds a lot more complicated than it is. TreeNode testRoot = new TreeNode();
testRoot.left = testRoot;
invertTree(root); // Stack overflow.
But that's not a tree, that's a cyclic graph, you may shout. That's true, but you still need to sanity-check your inputs.(I shouldn’t be arguing interview problems on HN, but I’ve had a few beers.)
Or at least to point out to the interviewer that your solution fails to handle unexpected input, and ask if they want you to add input validation logic, or if they are satisfied with the answer.