Create a stack and start inserting the nodes of BST in stack in in-order fashion

if elt/node that we are going to insert is larger than stack top, pop the top node and push the elt/node

else push the elt/node

Finally swap the values of first and last nodes in Stack (After the traversal stack will have two elements if adjacent numbers are swapped and stack will have three elements if non-adjacent numbers are swapped)

this is best so far. some optimizations like -:
we can stop early
I.e after finding first anamoly using your method (anamoly is "number whose next number is smaller than the itself", no need of Stack just variable could do).
Now we just have to traverse to find the number's position whose next number is bigger than anamoly number

Yep, the mentioned algo is not absolutely correct.
E.g. it shouldn't do pop(top) - push(current) in case of (there are more than one element in the stack and current.data > top.data). Otherwise the stack won't contain swapped elements at the end of the BST traversal procedure.

and it is also not necessary that you will find two buggy node always, there are many cases where you can only able to identify single buggy node.
consider the case when a node is replace with one of it's decedent

1. Find In order traversal of the tree.
2. In the inorder traversal, from left to right, find the first node which is misplaced(A misplaced element is one which is greater than its right element in the inorder matrix).
3. Now taking the element found in step two, traverse inorder array from left to right, and find the first element which is less than the element found in step two.

So the in actual implementation, for step two we traverse tree in order two find the node.
And two find the second node in step three we traverse in reverse inorder.

The two nodes swapped may not be adjacent to each other in inorder traversal. Instead when you find a node that is out of order do a binary search for the correct position of the node and swap with that node.

No, this works. The tree structure won't be changed. After the array is sorted, an in-order traversal of the tree will be done, and the i-th node in the in-oirder traversal will get the i-th value from the array. The problem here is that 1) you are using extra space (no one said you couldn't, but still); 2) sorting is O(NlogN) whereas this problem can be solved in O(N); and 3) even if you realize that you should use insertion sort because the list is almost sorted and end up with an O(N) sort here (since only O(1) elements are out of order), you're still doing 2 tree traversals, writing to an array, and doing an insertion sort when it's possible to solve this problem with just one tree traversal.

tarverse the tree in inorder nd preorder way nd store in two arrays.. now check for the incorrect number in inorderly filled array(as can be easily seen b'coz number must be sorted) and den swap dese two values in both tha arrays i.e. inorder traversed array and preoder traversed array.. and then using two traversal its easy to create the tree with the same structure as that of before..
p.s. two traversal are required just to maintain the structure or else only one can also work.. :)

The method works. Consider the isBST calls when we are traversing the right sub tree.
assume isBST (Tree, min, max);
For node 12, we get isBST (T, 10, INT_MAX); which is ok for 12
For node 8, we get isBST(T, 10, 12); which is not ok, as 8 < 10 (min). so we set the 2 nodes to be swapped as 8, 10.

In cases, if the swapped nodes belong to left and right subtrees, then isBST will encounter such anomaly twice , and we can simply set the nodes to be swapped as the nodes that caused those anomalies.

Do an in order element ...
misplaced element will be 2 elements
[1] one element will be greater than both of it's left and right
[2] 2nd element will be less than both left and right
Swap above 2 elements

What's Going On

Books

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Resume Review

Most engineers make critical mistakes on their resumes -- we can
fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.