Showing posts with label Leetcode 102: Binary tree level order traversal. Show all posts
Showing posts with label Leetcode 102: Binary tree level order traversal. Show all posts

Thursday, May 3, 2018

Leetcode 102: Binary tree level order traversal

May 3, 2018

Introduction


I like to work on three ideas to implement the algorithm called binary tree level order traversal. The peer gave me honest feedback after he mock interviewed me on May 2, 2018. It should take you less than five minutes to write, and then you have chance to move on the second question. But you actually did take 20 minutes.

To train myself to work on this algorithm, I plan to spend time to read a few hours on the discussion panel of the algorithm first.

Using extra node null to separate the level


I like to write a C# practice using extra node null to separate the level. Here is my C# practice.


Leetcode 102: Binary tree level order traversal

May 3, 2018


Introduction


It was my mock interview algorithm on May 2, 2018. I was told very honestly that I should memorize the common queue technique, using breadth first search, two loops, outer loop for each level, inner loop for each node in the level.

I decide to write a few solutions based on Leetcode 102 discussion.

Algorithm practice


Here is my C# practice to use the idea of two loops, outer loop for each level, inner loop for all elements in the level.


Wednesday, May 2, 2018

Why I work on mock interview and choose to be an interviewer?

May 2, 2018

Introduction


I had a mock interview this 10:00 PM. At the end of interview, the peer asked me why I choose to be an interviewer on mock interview. So I like to write a small research on this topic.

Will continue to work on it.

Sudoku solver algorithm 


Learning one algorthm very well. It takes determination to learn one algorithm over years since I could not believe that I am too busy and I have to constantly remind me to work on the same algorithm and get better and better. This will make my life as a software programmer much easy.

It is very important for me to learn the algorithm very well in order to sustain my career as a software programmer. I like the mock interview since I love learning and teaching, and also I do see the opportunities to improve through each mock interview.

I like to write down my experience of learning Suodku solver algorithm. I started to work on the algorithm and documented my practice in a few of blogs in 2015. I thought that I did very well to master the algorithm.

I got to know the truth once I start to practice mock interviews.  To work on the sudoku solver algorithm in mock interview, one time a senior developer who works for fortune 500 gave me a rating of 1 out of 4, and later in mock interview the peer told me that I should work on the structure of depth first search algorithm. I have worked on 10 rounds of mock interview, so I have chance to work on the mock interview over 20 times. I also asked the question on code review website.

It is very important for me to learn the algorithm very well in order to sustain my career as a software programmer. I like the mock interview since I love learning and teaching, and also I do see the opportunities to improve through each mock interview.

Learning and teaching 



I remember that I used to read a post by a high school teacher in early 2006 when I worked on computer science Ph.D. in Florida, who is my second elder sister Jianhua's friend. My sister Jianhua has over 30 years teaching experience. Basically the friend wrote in her post is that learning and teaching is kind of repetition. You got so many feedbacks coming in, bad or good. You have to work on those feedback, integrate them to next teaching or learning.

Learning algorithm is kind of the same thing. You practice the algorithm through mock interview, work with different people who is also hard working. And then you will get a lot of things to work on.

Do not stop. Keep continue. Work on the same algorithm again and again. You will find things to work on besides memorizing the solution. There are a lot of challenging issues.

My mental toughness


I had a mock interview with a friend on May 2, 2018. After more than one hour mock interview, the peer gave me honest feedback. I should memorize the common solution for those algorithms. If the competitor makes it in five minutes, I take 20 minutes, I will be in trouble. I will not have chance to get the second question.

Also here is the quote:"Must do interview questions
Each topic has some building block questions which end up being used by a lot of solutions. It is important that you already know solutions to all these problems so you don't waste time thinking up the solution to these problems and focus on the question interviewer has given you because you will be judged on that. I have included some of  these questions but if you go through Interview Bit in detail you would eventually come across/cover most of these questions"


I gave my thoughts as well. Here is my argument:

I always watch those tennis professional player up and down, so that I will not get disappointed too long if I fail. I used to hit rackets to the net but opponent complained to me. I like to practice better than real interview.


I like to practice than real interview



I like to write a few sentences for this argument. I do think that the more I meet peers through mock interview, I know better to do current job. My achievement is tremendous and make me grounded. I am not trying to achieve unachieveable target, serve others if need, teach or learn.

Share the experience


I like to document my own practice. It looks like that I am the luckiest one to be able to document my algorithm practice more than a few years. I treat the code like commodity. I can produce and consume any time. I do not need to worry too much how good my code is. If it is not, I will get better next blog or next practice. If it is good, then it will be such a fascinating marketing tool for me to get connected to more people.

Love to code every day


It is like sports activity. To maintain the physical health and mental health, I always play sport and also matches to encourage myself to get in teams, and continuously learn new things.

I got feedback from binary tree level order traversal from my mock interview performance on May 2, 2018 9:40 PM. I use extra node null to mark the end of level, but there is more popular trick to use a nested loop for the current level, and leave the outside loop for the level update. I missed that part, and the peer told me the truth, honest feedback. He asked if I like honest feedback or nice sayings. I chose the honest feedback.

I practiced the algorithm over 2 years ago. I just reviewed my past practice, and also spent hours to go over Leetcode discussion first time on this algorithm called binary tree level order traversal.

Here is my C# code written for binary tree level order traversal.

Here is my C# code written for zigzag level traversal.

Thursday, July 23, 2015

Leetcode 102: Binary tree level order traversal

July 23, 2015
Problem statement:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7},
    3
   / \
  9  20
    /  \
   15   7
return its level order traversal as:
[
  [3],
  [9,20],
  [15,7]
]
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
写代码发现很多问题. 程序要多写, 多练. 背算法可能也是一个途径. 从C++代码, 转化成C#, 犯了几个错. 一下学习到很多C#知识, 感觉不错! 练习6种方法. 
1. Solution 1: (push extra null node in the queue to divide level)
Read the blog:
convert it to C# code:
C# code passing leetcode online judge:
Solution 2: (using 3 variables to help queue to do BFS algorithm)
blog:
C# code:
Solution 3: DFS algorithm:
blog:
C# code: