Friday, July 27, 2018

Leetcode 543: Diameter of binary tree

July 27, 2018

Introduction


It is an easy level tree algorithm. There is a linear solution O(N), the idea is to traverse the tree using preorder, and then calculate the maximum path value along the way. In the recursive call of maximum path value, all possible diameter can be calculated each step.


My practice


Here is my C# practice with optimal time complexity.

I had so many issues to work on the coding. I like to share the submission results and remind myself to be humble, and work hard on easy level algorithms first.


Ideally I should spend less than 10 minutes and submit once to get accepted. Here are highlights I had in more than one hour practice.

1. Line 34, global variable needs to reset. Online judge reported error one node tree return 3.
2. Line 52, 53, left and right variable should be used, but my mistake is to call the same function again. Time limit exceeded.
3. I aimed optimal time complexity solution, but the code has some issues...
4. I wrote time complexity O(N * N) solution, and then ...




No comments:

Post a Comment