Tuesday, January 22, 2019

code review: longest univalue path

Jan. 22, 2019

Introduction


It is a good idea for me to train myself hard. One thing I can do is to study very good written solution, and try to improve it, or simplify the code.

Code review

Here is the algorithm written in a coding blog called  a casual coder.
Here is C# solution I like to review for longest univalue path.

I also write down my thought process as well.

Thank you for sharing longest univalue path. I played with your solution in the above a few times, it is hard for me to come out the good review, but I did something to share with you. 

The variable maxThruLeft is declared and calculated but it is no use in your code. Same as maxThruRight variable. 

If we use maxThruCurrent variable and apply it to every node in the tree using post order, then we can get longest univalue path. No need to use third variable called maxGlobal. 

The following is the code I rewrote and just provide you a second opinion. The code passes online judge. 

Here is C# solution I rewrite to simplify the code.


No comments:

Post a Comment