Showing posts with label Leetcode 250: Count univalue subtrees. Show all posts
Showing posts with label Leetcode 250: Count univalue subtrees. Show all posts

Thursday, April 5, 2018

Being interviewer: Leetcode 250: Count univalue subtrees

April 5, 2018

Introduction


It is my favorite algorithm in March, 2018. I had two practices on this algorithm, one is to write the algorithm in 30 minutes mock interview, and the second one is to review the peer's practice. I chose the algorithm to interview the peer on April 4, 2018 10:00 PM, but I could not give out the solution based on the peer's code.

Follow up 


Here is the code I reviewed in mock interview. The peer asked my advice, but I could not tell at that time the code was bug free.

Now it is 9:20 PM April 5, 2018, I like to write a C# solution based on the simple idea, using negative to represent that the tree does not have same value. Here is my C# code.


Monday, March 26, 2018

Being an interviewer: Leetcode 250: Count univalue subtrees

March 26, 2018

Introduction


I like to learn to be a good code reviewer. Sometimes I practice with a friend a few times, I start to learn how to help the peer to give some code review.

Code review


I spent over 60 minutes to work on the algorithm, the peer gave me very good advice how to make improvement. So I also like to work hard and give some code review.

I will document my code review here as well.


Here is the Java code I got from the peer and then I cleaned up test cases and analysis of the algorithm.

Here is my code review:

isUnivalTree function


You can simplify the function isUnivalTree using one statement, please see attached image of the highlight of your code:




Here is my advise how to write in one statement instead. I will explain why I like to do that.



The argument is this, left && right is not true, you do not need to check root value compared to left child value if there is one. So your logic includes redundant work. 


checkSize function


There are a few issues on checkSize API,



1. First the return type does not match API definition, people will surprise to get a return Node when the function name is called checkSize. You should return size instead of a Node. Why we need a Node, where to find the size in the Node object. 

Also the function name checkSize is not meaningful, it is better to call calculateUnivalSubtreeCount. 

2. Second, you have a giant expression I circled using red color. It is better to avoid negative checking. Think about default value, and then write a positive checking. 

  Here is the code I think with better presentation:


Clean up is a small good thing to do


I also like to point out that it is good habit to clean up document before sharing with others. Remove extra space, unrelated comment, and make the code clean and readable. Once you do it in every mock interview, you will start to learn how to act quickly. You will apply the habit to the work and also in the official interview.

Working hard does not mean staying up 1 AM or 2 AM. It takes time to build up good knowledge of data structure and algorithm. But builing a good habit just takes determination and good mind of making smart choice, that is my thinking. Hopefully I can bring your attention to the issue.

Leetcode 250: Count univalue subtrees

March 26, 2018

Introduction


It is a medium level algorithm. I was asked to solve the algorithm in the mock on March 25, 2018. So I had chance to learn from the peer.

Code review 


Here is my C# code. I got the idea to solve the problem since I studied another tree algorithm
Leetcode 230. Kth Smallest Element in a BST on March 23, 2018.

Being interviewee: Leetcode 250: Count univalue subtrees

March 26, 2018

Introduction


It was my algorithm called Leetcode 250: Count univalue subtrees.

My practice


First I was asked to work on the function to check if binary tree has same value. And then I was told to work on the algorithm to count univalue subtrees.

Here is my practice.

I came out the working solutions for both algorithms. But the first one the peer told me to simplify the algorithm. I do not need to use extra two arguments: hasDefaultValue, defaultValue.

I spent 28 minutes from 9:18 PM to 9:46pm to work on the first algorithm, including discussion with the peer. And I spent 41 minutes to work on the second algorithm, and also had a discussion with the peer on different test cases.