Friday, 24 April 2015

Past Exams

I'm starting to study for my final exam now. I got an interview on Monday :) so excited and I'm having a hard time focusing on studying so to keep myself motivated, I decided to post my solutions. I don't think anyone is reading my blog (o_O) but if you dear reader find any mistakes, do mention.

So the question is to determine whether or not a binary tree is "complete". Quick google search suggested complete binary tree is one where all nodes if applicable have 2 children. So minimum height binary tree. In class, we briefly touched on being balanced. Interestingly, the problem requests that I recursion NOT be used (which I thought was really weird).

It was a fairly fun problem. I used some sort of queueing system. I don't think the efficiency is good and the definitely a lot of room for improvement... but I do restrict my time spent on each problem (like on tests) to around 20 min (excluding typing problem time and debugging after to "verify" my solution since we don't get answer sheet)....

The function is called is_complete(root) and takes a BTNode. I think at the end, the restriction made me think of tree traversal differently. Definitely an interesting problem. I think I will get a recursive version up too (in a while). nvm I'll just explain the recursive version (too lazy to actually code it).

For the recursive version, you can code height(root) which returns the maximum number of paths ("non-reversing") in the tree leading from root to leaf. You also recursively find height2, which is where the first None occurs (so return min on "pseudo leaf" where pseudo leaf is where at least one of the two childs is None). Compare the two... and return False if the difference is greater than 1 (2 or more). I'm sure there is a better way though since you are traversing some parts of the tree multiple times. But I think the basic idea is there.

There are other stuff on the following... which is just some soln to other problems.


No comments:

Post a Comment