LeetCode 235. Lowest Common Ancestor of a Binary Tree in Java

In this post we will solve LeetCode 235. Lowest Common Ancestor of a Binary Tree problem using Java.

```Given a binary search tree (BST),
find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia:
“The lowest common ancestor is defined between two nodes p and q
as the lowest node in T that has both p and q as descendants
(where we allow a node to be a descendant of itself).”

Constraints:

o The number of nodes in the tree is in the range [2, 10^5].
o -10^9 <= Node.val <= 10^9
o All Node.val are unique.
o p != q
o p and q will exist in the BST.

Realated Topics:

o Tree
o Depth-First Search
o Binary Search Tree
o Binary Trea
```

We are given a Binary Search Tree (BST) and the pointers / references to two nodes in the tree. We need to return the Lowest Common Ancestor (LCA) node. Continue reading “LeetCode 235. Lowest Common Ancestor of a Binary Tree in Java”

Brackets

Good day! In this post we will attempt to solve the Brackets problem from Codility_.

```A string S consisting of N characters is considered to be properly nested if any of the following conditions is true:

o S is empty;
o S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string;
o S has the form "VW" where V and W are properly nested strings.

For example, the string "{[()()]}" is properly nested but "([)()]" is not.

Write a function such that given a string S consisting of N characters,
returns 1 if S is properly nested and 0 otherwise.

Write an efficient algorithm for the following assumptions:

o N is an integer within the range [0..200,000];
o string S consists only of the following characters: "(", "{", "[", "]", "}" and/or ")".
```

We are presented with a string. We need to determine if the different types of brackets match. If interested in this problem I suggest you read the current and full description at Codility_. Continue reading “Brackets”

High Point in Array

Good morning! It is Friday and the weekend is just around the corner. In the Twin Cities of Minneapolis and St. Paul we received our first snow dusting of the season. Yesterday early morning I drove my wife to a healthcare facility to get a radiological exam. Traffic was heavier than what we expected. We got back home around 09:00 AM. The rest of the day ended for me at 19:00 PM. It was a very long and exciting day.

As the weather changes, I have slightly modified my workdays. I work in 2-hour blocks. The blocks are dictated by the EyeDefender app. I tend to work four to five blocks during the week and one to two on weekends. After each block I get on a stationary bike and pedal backwards for about two minutes and forward for six. This gives me over half an hour of exercise per day and half on weekends.

Starting this week, I will not stop immediately after EyeDefender tells me to do so. Will continue with the bike exercise after each block. Doing this will reduce the blocks to four on workdays. I guess it is better to continue working past the two hours until I reach a normal breaking point based on the task at hand. Will let you know my findings as far as productivity is related early next year.

One last thing, most of the times that I generate a post, I check on the number of subscribers. Today’s count is 10,025 subscribers Thanks a lot!!! This implies that in the very near future I will, in addition to this post, start a YouTube channel with similar content or will just switch to YouTube and discontinue this blog. One way or the other, I will be getting the necessary video hardware and software tools, and will start experimenting with them. I am planning on having the YouTube channel up and running by January 2022. Continue reading “High Point in Array”

Short Palindrome

Good morning! It is a cold and dark Sunday morning in the Twin Cities of Minneapolis and St. Paul in Minnesota. The silver lining is that the forecast for today calls for a high temperature of 80F. Perfect day for grilling outside and perhaps drink one or two adult beverages.

After finishing my 2-hour block, my wife and I will complete our grocery shopping by stopping by Trader Joe’s in St. Paul. It seems that lately we are doing groceries every other weekend. That saves around five gallons of gasoline every month.

It is too early (dark and cold) for box elder bugs to be out and about. As it gets warmer they seem to appear from nowhere. We are planning on grilling outside so they will become a nuisance. In the afternoon hours most homes in our area have a few hundred box elder bugs on the walls. As long as we do not have to interact with them, let them be. Continue reading “Short Palindrome”

Java Date and Time

It is a sunny day in the Twin Cities of Minneapolis and St. Paul.

Earlier today I read an article on Medium that described how bad are the crypto currencies and which actors are making money of it. I missed to bookmark the article. While writing this post, in an attempt to locate the article, I did a Google search using the following string: “medium crypto currency”. It returned several articles. I did not spend the time looking for it. That said; if you are investing on crypto currencies, perhaps it would be a good idea to read some of the articles that have reasons why you should stop doing so. That said, I have invested a few dollars on crypto currencies. My reasoning was to better understand what Bitcoin, among other crypto currencies is all about.

Today is the birthday of the wife of one of my wife brothers. We are invited later this afternoon. Hopefully the weather will hold. It is nice to attend a BBQ on a sunny day.

Today and for a few more days, I will attempt to solve some problems in HackerRank. In the past couple years I have concentrated on solving problems from LeetCode. Continue reading “Java Date and Time”

Reverse String II

The end of my workday is approaching shortly. Hopefully I will be done with this post soon; otherwise will finish it tomorrow.

Earlier today I was snooping around in the LeetCode website. For the first time I ran into a page that shows every submission you make for a problem. Perhaps I am mistaken, but it seems that the site counts as attempts every submission you make. For example, you ran your first pass and let’s say it is accepted but you want to learn more by improving performance. You may even generate one or more implementations. It seems that they count against you. This seems awkward to me. If I am trying to learn I should not be penalized. If my assumption is incorrect, PLEASE LEAVE ME A NOTE BELLOW SETTING ME STRAIGHT. Continue reading “Reverse String II”

Find Peek Element

Good day! Hope you are doing well. Today is July 13th, 2021. We are approaching midsummer. Not sure where time is going.

As I mentioned a few posts ago, I attempted some daily problems posted by LeetCode. At this time I am somewhat busy so I have missed a few. Hopefully in a month or two I will have enough time to work on a problem a day; at least the easy and medium. The others may take a couple days. Continue reading “Find Peek Element”

Find Leaves of Binary Tree

Hi gals and guys! It is Friday afternoon and we have a long weekend ahead. Hope you enjoy Independence Day in the USA. The weather in my neck of the woods will be in the lower 90’s F thru Monday. On Tuesday the temperature will drop a few degrees and it seems we might receive some precipitation in the Twin Cities of Minneapolis and St. Paul.

On a separate note, it appears that the UPS attached to one of my computers is starting to fail. One of the internal batteries does not seem to be charging as it should. I guess it is time to replace the unit. I believe I purchased the UPS about 10 years ago. OK, I just ordered a replacement on Amazon. The UPS should arrive on Friday this week. Continue reading “Find Leaves of Binary Tree”

Largest Odd Number in String

Good day! Hope your day has started on the right note. The weather has been behaving somewhat strange this season. We started with very hot days and little precipitation. Such conditions caused many patches of dry grass to appear on lawns all over the Twin Cities of Minneapolis and St. Paul. In the past few days it has been raining on and off. That has helped lawns. I checked the weather forecast for today and my wife and I were planning on having breakfast and then head out for a walk. The forecast was off. We had precipitation for the first few hours, so after breakfast I showered, got dressed and went down to my home office.

In a previous post I mentioned that I was going to participate in a weekly programming contest. The contest was scheduled for last Saturday. When I woke up we had rain and thunderstorms. In the past decade or so I have lost two KVM switches. After replacing the second switch I decided to shutdown all my computers when there is a thunderstorm in the area. I canceled my participation in the contest. Will check if there is one this weekend. Will let you know my findings. Continue reading “Largest Odd Number in String”