We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I had changed my methodology midway and did exactly what you suggested and got through all test cases but I'd still like to know what was wrong in the first way I came up with.
It went like this. checkBST calls a helper function check with parameters root and a value equal to 0. The check function returns val as it is for NULL root, and increments val if the subnode is greater for left subnode. After this it recursively calls itself for the left subnode sending the left subnode and val also as a parameter. Similarly for the right side. The function returns val which is checked for 0 in the original checkBST.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Is This a Binary Search Tree?
You are viewing a single comment's thread. Return to all comments →
I had changed my methodology midway and did exactly what you suggested and got through all test cases but I'd still like to know what was wrong in the first way I came up with.
It went like this. checkBST calls a helper function check with parameters root and a value equal to 0. The check function returns val as it is for NULL root, and increments val if the subnode is greater for left subnode. After this it recursively calls itself for the left subnode sending the left subnode and val also as a parameter. Similarly for the right side. The function returns val which is checked for 0 in the original checkBST.