English 中文(简体)
Find two numbers in a binary search tree that add up to a third number
原标题:

You are given a BST of numbers. You have to find two numbers (a, b) in it such that a + b = S, in O(n) time and O(1) space.

What could be the algorithm?

One possible way could be two convert the BST to a doubly linked List and then start from the front and end:

if front + end > S then end--

Or:

if front + end < S then front++
最佳回答

As others mentioned, you can t solve this in constant O(1) space. Furthermore, all the other solutions currently suggested use at least O(log^2 n) space, not O(log n) space: the stack has O(log n) frames and each frame has a O(log n) sized pointer.

Now, the currently accepted solution by @dharm0us destroys the BST by converting it into an array. This is unnecessary. Instead, use two iterators, one doing an in-order traversal and one doing a reverse-order traversal, and look for two numbers the same as you would in an array. Each iterator has a stack with O(log n) frames with each frame holding a O(log n) size pointer for total space O(log^2 n). The time is clearly linear O(n).

问题回答

I was asked this question in an interview recently. When I was stuck, I was given a hint.

Hint: Let s say you need to solve this same problem for a sorted array, how would you solve it then.

Me: Keep two pointers. One at the beginning, another at the end. If the sum of elements at those pointers is less than the required sum, move the front pointer towards right, else move the back pointer towards left.

Interviewer: How could you do the same thing for a binary search tree?

Me: Do an in-order traversal, and save the pointers to nodes in an array. And use the same logic as in the case of arrays.

Interviewer: Yes, that works. But the space complexity is O(n). Could you reduce it?

Me (after a lot of time): Ok, convert the BST in a doubly linked list, using this algo. And then use the same logic as in the case of array. Space comlexity will be O(lg(n)) due to recursion.

Try as I could, I m not sure this is possible with a binary tree that has no parent pointers. O(1) space means you can neither use recursion (the stack growth is O(log n)) nor copying to a doubly linked list (O(n)).

The method you allude to is an O(n) time complexity solution but not with a normal binary tree. In fact, I answered a similar question in great detail here. That was solved with O(n) space but only because they weren t initially sorted.

It is possible with a tree containing parent pointers. If the child nodes have pointers to their parents, you can basically treat the tree as a doubly-linked list processed iteratively.

To do that, you run the start pointer down to the leftmost node and the end pointer down to the righmost node. You also maintain two variables to store the last movement (up or across, initially up) of each pointer so you can intelligently select the next move (the front++ and end-- in your question).

Then you can use the current pointers and last movements, along with the current sum, to decide which pointer to move (and how).





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签