I was trying out a problem (irrelevant) on LeetCode — 1372. Longest ZigZag Path in a Binary Tree. I posted this question on the forum but got no reply. Hence, I am asking the same question here.
I noticed some weird behaviour when using the max function. I took a max in two ways:
Solution 1: max(1 + a, 1 + b)
Solution 2: 1 + max(a, b)
Both of these do exactly the same task of getting the 1 + max
. However:
- Solution 1: — Runtime Error!
- Solution 2: — Accepted
The full code is also given below:
The details for Solution 1 are given below:
- Runtime Error Message:
AddressSanitizer: stack-overflow on address 0x7ffee1685ab8 (pc 0x7f4840414c34 bp 0x7ffee1686300 sp 0x7ffee1685aa0 T0)
- Failed Input
If you notice, the code for both of them is exactly the same except for line 13. Could someone please tell me why Solution 1 fails? It really makes no sense to me.
Thank you for all your help!