Saturday, March 9, 2019

1008. Construct Binary Search Tree from Preorder Traversal

March 9, 2019

Introduction 


It is a medium level tree algorithm. I learn a lesson today and I like to share what I learn. It is so much enjoyment that I find something to work on.

Performance


I wrote the solution in the weekly contest 127, and it took me 52 minutes. I came cross so many errors, stack overflow, and wrong results. I just could not believe that it took me so long.

After the contest, I checked ranking no. 1 - Neal wu's code, and it took him 3 minutes 16 seconds.

What makes huge difference? He just chose to use a list data structure instead of array. He just push the original list into two separate data structure.

I chose to use the array, avoid using extra space. And then there are two user cases. Left child is missing or not missing.

So in other words, it is mindset.

When I practiced tree algorithms last year, I had same issues; it took me over one hour to work on one tree algorithm using array and two indexes to specify subarray.

Source code


Here is the page to access ranking and source code.

Here is ranking No. 1 code for Leetcode 1008.


Here is my code.



Actionable Items


Now it is 3/9/2019 11:33 PM

I need to learn to use data structure to simplify the task, avoid tricky code with stack overflow, wrong answer, and extra user cases. It takes me a lot of time to debug the code in the contest.

No comments:

Post a Comment