Showing posts with label Level Order Traversal. Show all posts
Showing posts with label Level Order Traversal. Show all posts

Saturday, January 16, 2016

Leetcode 314: Binary Tree Vertical Order Traversal

January 16, 2016 

First time after 9 years, I installed Eclipse, J2SE, and then, set up Java developer IDE Eclipse, ran the first Java program. Such a great ride to enjoy Java programming. Still remember that in 2006 - 2007, spent over 10 hours daily to read/ run/ maintain software using Java programming language, when I worked for a small startup called Siva Corp Inc. in the city of Delray Beach, Florida, USA. 

Just bring all my good memory back about programming using Java.


http://buttercola.blogspot.ca/2014/09/leetcode-n-queens.html

Leetcode 314:
https://github.com/jianminchen/Leetcode_C-/blob/master/BinaryTreeVerticalOrderTraversal_314.java

Another blog using C++,

http://buttercola.blogspot.ca/2014/12/facebook-print-binary-tree-in-vertical.html

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:

Tuesday, June 9, 2015

Leetcode: zigzag order traversal of binary tree

One more C# practice on level order traversal. So excited to read my past practice in 2015, April 29. Cheers! 

April 29, 2015 

I Spent a few hours to study the question. The website I read:
One thing I do not like is to use two while loop, and since it makes me wonder how to make it more simple and readable.
This is a better solution, I like it more:
Here is C# code I write and test on April 29, 2015:
https://github.com/jianminchen/zigzagOrderTraversal/blob/master/Program.cs


Follow up 


May 3, 2018

I spent over 30 minutes to review the C# code writting in 2015. I am not happy about the coding style, and also the code is not easy to read. I need to debug the code, and then update readable variable names, and understand how to apply stack to store nodes for each level.


Here is the C# code I ran and passed all test cases.