Tuesday, May 31, 2016

Leetcode 103: Binary Tree ZigZag traversal - a practice

May 31, 2016

Review the blog:
http://juliachencoding.blogspot.ca/2015/06/leetcode-zigzag-order-traversal-of.html

practice in 2015:
https://github.com/jianminchen/zigzagOrderTraversal/blob/master/Program.cs

Warmup practice on May 31, 2016, write a C# solution again:
https://gist.github.com/jianminchen/3723020cd9757f85d0ceb383da425c6c

Practice notes:

1. source code line 107 ,  Stack, use ref, pass reference.

Lookup why Stack is different from Array. Array reference is passed automatically.

Read the blog:
http://stackoverflow.com/questions/967402/are-arrays-or-lists-passed-by-default-by-reference-in-c

2. Extract a function named swap(), actually, the last practice:
https://github.com/jianminchen/zigzagOrderTraversal/blob/master/Program.cs

line 57, Stack<int> tmp is declared, but line 92 tmp is used. The scope is too large to pay attention. So, extract it to a function.

3. The last practice first while loop on line 59:
https://github.com/jianminchen/zigzagOrderTraversal/blob/master/Program.cs

It is confusing, so in current practice, line 72, while(true),
line 72: while(true)

later, on line 112, break the while loop.
line 112: if (currLevel == null || currLevel.Count == 0)
113: break;

4. line 61, if (root == null), return empty list instead of null pointer.

Nov. 14, 2016
Google/Bing Search Result:
zig-zag traversal of a binary tree in c

Review the blog, and then, need to document about array reference passing in C#, see stackoverflow article:
http://stackoverflow.com/questions/12757841/are-arrays-passed-by-value-or-passed-by-reference-in-java




No comments:

Post a Comment