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.
This is great! I coded the solution to this problem in accordance with your comment!
prevVal=-1resultVal=1defcheck_binary_search_tree_(root):inOrder(root)ifresultVal==1:returnTrueelifresultVal==0:returnFalse# perform an in order traversal definOrder(root):ifrootisNone:returnelse:inOrder(root.left)globalprevValifprevVal<root.data:prevVal=root.dataelse:globalresultValresultVal=0returninOrder(root.right)
With your comment, I coded the following solution in python3:
flag=Truepre=-1# As all data are >= 0 so set pre = -1definOrder(root):globalflag,preifroot.left:inOrder(root.left)ifpre<root.data:pre=root.dataelse:flag=Falsereturnifroot.right:inOrder(root.right)defcheck_binary_search_tree_(root):inOrder(root)globalflagreturnflag
I did a similar thing (except that the list was in descending order). My recursive algorithm had a flaw that appeared to be too difficult to waste time fixing.
Note that I am more used to C so I didn't bother with the C++ libraries.
You can just perform DFS and check if data is in ascending order. No additional data structures/parameters needed. Just one static variable (yep, I am a C programmer :))
Great solution based on the constraints: 0 <= data <= 10^4. This is to utilize BST inorder traversal property. If the data can be min int, then the solution won't work.
Inorder traversal of BST is always sorted. So you don't have to put elements into list. Store maximum element so far in a variable (say maxSoFar) and then just check if currentNode.data <= maxSoFar. It'll save you from iterating list.
I also did the same but some test cases are not working .Here my code-
bool func(vector v)
{
int count=0;
for(int i=0;i v;
if(root==NULL)
return true;
if(root!=NULL)
{
checkBST(root->left);
v.push_back(root->data);
checkBST(root->right);
}
return func(v);
did the same dude !!!!!! 2 cases failed in recursion,didn't know the reason and sample input was also confusing and accidentally i reached this solution while understanding the sample input ... ;)
You don't need to create list or array while doing inorder traversal. The whole point is to compare current and last element, if last elment is greater or equal to current return false. Just use one wrapper int, or Integer static variable to keep track of last visited.
Isn't necessary to store the values in a list, just keep track of the previous visited value (inorder sequence), and if your current visiting value is less than it means that isn't a BST
Is This a Binary Search Tree?
You are viewing a single comment's thread. Return to all comments →
Well , I just did an inorder traversal, stored them in a list and checked if they are in ascending order. If yes, then its a BST , else it isn't.
This would work for all values (signed or unsigned) a node can possess.
When compared with recursion (which has stack depth), space complexity remains the same in worst case.
Time complexity also remains O(n) , only difference being that mine is a 2-pass solution.
I did the same
Even I did the same!!
I did this as well!
I used the same method like you, but I passed all solutons. However, I have no idea why my recusive method can't pass all.
Can you share your recursive code ?
Hey ,Even I tried the recursive method but all the test cases didnt pass, Please check the code
Consider the below 2 types of trees, you will know where you went wrong.
Case 1 :
Case 2:
Alright I got it we have to find the minimum from left sub tree and the maximum from the right one Thanks!
Yes correct !
Isn't it that all nodes in left tree should be less than root and right should be greater than root. Than why we need to find min and max.
Is it not the other way? minimum from right sub tree must be greater than root and maximum from left subtree must be less than root
Got it. thanks a lot.
Thanks for case 2 visualization, this explains why my code failed a bunch of test cases.
Can anyone please tell me what's wrong in his code? What t understand from these two test-cases?
Queue ino=new LinkedList(); boolean checkBST(Node root) { inorder(root); int[] arr = ino.stream().mapToInt(i->i).toArray();//convert ArrayList to primitive int array int[] brr = ino.stream().mapToInt(i->i).toArray(); //ArrayList brr=new ArrayList(arr); Arrays.sort(arr); /* for(int i:arr) System.out.print(i+","); System.out.println(""); for(int i:brr) System.out.print(i+",");*/ if(Arrays.equals(arr,brr)) return true; else return false; } void inorder(Node root) { if(root==null) return; inorder(root.left); ino.add(root.data); inorder(root.right); }
Note, I haven't seen your code but here's a tip:
You need to also think about all previous values higher in the tree, and not just the the current node, and the top of the left and right subtrees.
no need for a traversal. Take the node->data of each node directly from the vector values given in private. Rest logic is spot on. Did the same :)
This is great! I coded the solution to this problem in accordance with your comment!
My original solution is:
With your comment, I coded the following solution in python3:
I did a similar thing (except that the list was in descending order). My recursive algorithm had a flaw that appeared to be too difficult to waste time fixing.
Note that I am more used to C so I didn't bother with the C++ libraries.
You can just perform DFS and check if data is in ascending order. No additional data structures/parameters needed. Just one static variable (yep, I am a C programmer :))
Great solution based on the constraints: 0 <= data <= 10^4. This is to utilize BST inorder traversal property. If the data can be min int, then the solution won't work.
This is a wonderful recursive solution.
Wow,,,, How could you tinker this method,, What a brilliant!
Inorder traversal of BST is always sorted. So you don't have to put elements into list. Store maximum element so far in a variable (say
maxSoFar
) and then just check ifcurrentNode.data <= maxSoFar
. It'll save you from iterating list.thanks,
i tried same thing but many test cases fails... help me to come out plz.
I also did the same but some test cases are not working .Here my code- bool func(vector v) { int count=0; for(int i=0;i v; if(root==NULL) return true; if(root!=NULL) { checkBST(root->left); v.push_back(root->data); checkBST(root->right); } return func(v);
}
You want this...
did the same dude !!!!!! 2 cases failed in recursion,didn't know the reason and sample input was also confusing and accidentally i reached this solution while understanding the sample input ... ;)
You don't need to create list or array while doing inorder traversal. The whole point is to compare current and last element, if last elment is greater or equal to current return false. Just use one wrapper int, or Integer static variable to keep track of last visited.
But its not always true.
Consider this case:
This would return false. Inorder 8 -> 18 -> 10
18 > 10
As long as the last element is the last elemented "visited" and not last element passed by.
Yes, but as long as we return max values from left subtrees and min values from right subtrees and make sure they follow the rule.
I didn't do that. I just did inorder traversal recursively and made sure it was all ascending order. Passed all cases.
Mine is also a similar idea.
I think you can improve your space complexity only using a variable and comparing current node with his prev node.
Was able to solve it because of your comment. thanks a lot. Here's my code.
Did the same thing.
I did the same. But it's not working.
Here is my code. Can you review it please?
Try this
Isn't necessary to store the values in a list, just keep track of the previous visited value (inorder sequence), and if your current visiting value is less than it means that isn't a BST
Thanks for your insight! My code worked!