My first thought is using BFS to process the tree level by level. However, a regular BFS needs assistance of a queue, which is not constant space.
Anyway, the first problem (assuming a perfect binary tree) is relatively easy. By taking the similar strategy of solving Symmetric Tree problem, I got a very neat approach:

There are some repeated linking between node 7 and 8, 9 and 7. The result is correct, but when the tree size increases, this repeated linking cost may increase, too.
2. Definitely, this approach can't be applied to an arbitrary binary tree.

So, what can we do when it comes to any binary tree?

We can still try to process the tree level by level, but without a queue.
The trick part is that when we process the nodes of next level, we make sure that the current level has already been linked.
For example, when we deal with the third level, nodes 2 and 3 in level 2 have already been linked, then node5.next = node2.next.left( if not null), otherwise, keep searching towards right.

Sunday, November 25, 2012

Problem Description:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
It is the same problem as that in the book Cracking the Coding Interview Question 1.7
At first glance, a quick solution coming to mind maybe using two arrays to record the rows and columns that should be set to zeros respectively. And this solution needs O(m+n) space. As suggested by leetcode OJ , can we use constant space ?

Well, yes we can. The tricky part is that we use the first column and the first row of the input matrix to record the rows and columns that needs to set zero. Another point is that since the 1st row and 1st column are intersected at cell (0,0), so matrix[0][0]=0 can't tell us the 1st row or 1st column or both need to be set 0. In my implementation, I use two extra variable firstLine and firstCol to record it.

Here's the code, well, I concede it's a little messy. If there's other neat way to do it, I will appreciate it if you let me know.

The Expected "Largest Rectangle" in this histogram is in shaded area which equals 10 unit.

As I stated at the beginning, there's O(n) solution for this problem by taking advantage of stacks. I'm not going to the details of that algorithm, assuming we now have a function

int findMax(int[] heights)

which can get the max rectangle area in the histogram in O(n) time, then we can easily solve the Maximal Rectangle Problem.How?

We need to construct an assistant matrix to help usrecord the "histogram" of each row. As shown in the red rectangle, the corresponding row in the assistant matrix is the histogram of that row in the original matrix.Then we just need to process the input matrix row by row, and for each row, we use findMax function to calculate the largest rectangle area:

heights.push(height[i]); indices.push(lastIndex); } //note:if current = top , just ignore it. } /** * note: after processing, there still maybe elements in the stack. * but they MUST BE in ascending order in the stack from bottom to top. */ while(!heights.empty()){ int temp = (height.length-indices.pop())*heights.pop(); if(temp>max) max = temp; } return max;

}That's pretty much my general idea. In order to make it more efficient, we can merge findMax function into our maximalRectangle(). Therefore we don't need to create new stacks by scanning each row. And we don't need to traversal each row twice either.