-
Notifications
You must be signed in to change notification settings - Fork 0
Day 1 to 10
For the first day, I begun the LeetCode Binary Tree Module. The first problem was a Preorder Traversal of a binary tree, which they recommended be solved iteratively.
To warm myself up on the first day – and because I haven't done this type of thing in ages – I worked on a recursive solution first (tomorrow will be my iterative solution). The start was a bit rocky, but I managed to find the solution after a short while. I feel I might benefit from getting into n-back training again to improve my short-term memory.
Today was definitely a bit more challenging because my family has been in transit to Rome – so maybe starting this whole thing the day before our vacation to Europe wasn't a good idea – but I managed to complete this day regardless!
Still worked on the same problem, but with an iterative solution this time, which was definitely longer yet apparently faster than the recursive one. My familiarity with the problem might have made it easier, but sitting down to visualize the problem and look at binary trees while I thought up my solution definitely helped. It definitely helped as well to have been familiar with stacks and queues for this one.
Wasn't able to complete yesterday, but I'm giving myself a pass (for now) because I'm supposed to be on vacation. Anyhow, I was able to solve the Binary Tree Inorder Traversal problem for today.
I did just the iterative solution since a recursive one would have been trivial and easily derived from my recursive solution for the last problem. Took a bit longer on this one, but I am beginning to feel more comfortable navigating trees. I struggled again at first, but staring at a sample binary tree and talking myself through my planned algorithm really helped.
Completed the last of the depth-first search problems – this one being a postorder traversal.
The solution still followed the same general flow as the previous problems with only slightly varying conditions. Nothing much new here. I think at this point though I ought to pay equal attention to the articles that follow the problems to supplement my understanding and open my mind to different ways of approaching these kinds of problems.
This one might have been the hardest yet (though none have yet been really difficult); probably because this problem is already a deviation from the prior ones – a breadth-first search, rather than a depth-first search. Though none of the problems have yet really fallen outside the scope of what I've already learned, so I haven't been able to break new ground yet, but answering these are an exercise in my problem solving skills nonetheless.
I still aimed for an iterative solution, which I was able to implement with a queue. (Side note that I very nearly was not able to complete today because I was already quite sleepy while answering this. I should consider completing these earlier in the day.)
Another binary tree problem, but this one asked for a recursive solution.
Again, still fairly easy — and made easier because the answer was already written in the article describing top–down and bottom–up recursion presented before the problem. Got to an answer fairly quickly, but I found that my solution was a bit verbose compared to the faster submissions. I will try be more conscious now of writing just what is necessary.
I am not particularly proud of how I came to my solution for this one. I submitted too much and too often without really thinking about the changes I made each time. Even then, I eventually settled for the easier solution than the one I wanted.
That being said, the code for that was more readable than the latter, which might have outweighed any of the benefits of the latter. It all became immaterial though because the ideal solution was both more readable and more performant than both the solution I wanted and that I settled for.
The issue definitely was my wanting to just get my problem for the day over with. I think it would be better for me to just forego the problems for the rest of my vacation so as not to pressure myself into completing them at the expense of learning and thinking about each problem.
Back from my break. This one was definitely more fun than I remember (I had answered this before and struggled much more than I did now). I think I was able to come up with an elegant and readable solution compared to the last time — hopefully owing to how much I've grown as a developer.
A couple of things: (1) I answered this in the morning, so perhaps there was not as much pressure to finish as before; (2) this being in the morning might have coincided with my being a morning lark, allowing me to solve the problem much more efficiently; (3) I might have just missed doing this, so my excitement carried me. Anyhow, my main takeaway is that I should ideally be completing these in the morning.
Regarding the problem itself however, it was a simple matter of bottom–up recursion for me. Alternatively, it could have been solved iteratively with a stack.
Construct Binary Tree from Inorder and Postorder Traversal
So I took a (weekend) break after already having come back from my break. 😅 This is the real beginning though! Back home, back to work, and back to regular programming so I WILL be able to keep this in order now. One problem per day. Every (week) day. No breaks.
Today had to have been one of my most satisfying solves. The logic of my solution was right from my first run. I was definitely off on syntax and on one edge case, but the logic is what I am practicing, and today, I ran away with it! The problem definitely stumped me at first, but again, just looking at binary trees and their corresponding in- and post-order traversals and trying to establish patterns from there was the key. Visualization is a powerful tool!
Construct Binary Tree from Preorder and Inorder Traversal
Not much to write about here. Almost the exact same problem and attacked and solved the exact same way. Tried looking over the fastest solution and made me realize I could have used a dictionary to speed things up too. The methodology of that solution was also a bit different because it allowed an avoidance of list slicing which is probably a bit more costly. Interesting stuff.