You are viewing a single comment's thread. Return to all comments →
I started with this aproach too, however this only checks that the children nodes are correct for the parent node only.
Consider the case where your root node is 100, then we traverse down the left side a couple of times and come to a node that is 50. Let's say the left child is 10, and the right is 999. This will pass because the code only checks the immediate children, however it is not a BST because 999 is much bigger than the root node, 100.
I didn't see your comment before and now that you've explained it, I can't believe I didn't find the problem earlier :D
robertram's solution does consider the problem of checking that the children nodes are correct for all parent nodes upto root.
Note that the 'min' and 'max' variables get updated when you call the first two recursions. After you pass root.data to min and max once, all subsequent calls will share the same min and max, not the Integer.XXX_VALUE defined.
thank you.really the best explanation.
Thanks so much! I was struggling on this problem for a while and couldn't figure out what was wrong with my algorithm.
correct, and I have my solution that way. When debugging I realized it.
Thanks a lot. I missed this point and hence my code was not passing all the testcases.