Review blogs - Leetcode binary tree preorder traversal
C# practice:
use stack, and also know the order to push child nodes: right child first, and then left child next.
Code is here.
Question and Answer:
1. How is the practice?
Julia first thought about using queue to implement the solution, left child first, and then right child next. But, it does not work, since left child's left child should go to traversal first before right child. So, she had to stop.
Then, she tried to look into iterative solutions in general through Google, and also checked her previous practice.
Next time, go through a test case, do some analysis on the test case. Draw some diagram, help yourself to analyze, at least behave
2. Things to work on through the practice?
queue -> stack -> opposite order to push into stack
3. Can you describe the process using your own words, a few of drawings to help the thinking process?
First, read the wiki article about stack.
Let us work on a simple test case:
The preorder traversal of tree: 1 2 3 4 5 6 7
When the root node 1 is visited, 2 and 5 should be added to some data structure, 3 and 4 will be added after 5, but output of 3 and 4 should be before 5.
In other words, the data structure should accommodate last in first out feature. So, it is stack!
Once stack is chosen to use, then work out the simple tree first with 3 nodes:
preorder traversal: 1 2 5
so, push 1 into stack, and then, pop out. 5 is pushed in stack first, and then it is turn of 2.
Next, work on the test case: tree - preorder traversal 1 2 3 4 5 6 7
1 is pushed into stack,
1 is on the top of stack, 1 is popped out,
1's right child 5 is pushed into stack,
1's left child 2 is pushed into stack,
2 is popped out from stack,
2's right child 4 is pushed into stack,
2's left child 3 is pushed into stack,
3 is popped out from stack,
4 is popped out from stack,
5 is popped out from stack,
5's right child 7 is pushed into stack,
5's left child 6 is pushed into stack.
6 is popped out from stack,
7 is popped out from stack.
3. Most favorite problem solving using stack?
HackerRank: string algorithm - Reverse Shuffle Merge (IV)
warmup practice - C# practice code
Second practice:
C# code
so many bugs in second practice:
1. confused on skip, add array --, ++; line 71, line 80 did the opposite.
2. string reverse, char[], string, Array.Reverse etc. lookup
3. add one more test case: "abcacb",
b in stack, c in stack, then, run into a, pop up c, and pop up b, let 'a' in the stack. Two in row pop up in stack. While loop is tested on line 64 - 67.
4. while statement from line 64 - 67, fix compile error.
both are ok to compile:
(char)stack.Peek() > runner)
(char)stack.Peek() - runner > 0)
Third practice:
The code passes HackerRank online test cases as well.
A few good changes:
1. add comment from line 67 - line 72, test case: "abcacb",
use test case, stack top -'c' is removed, help to ensure the code is correct.
2. line 73, avoid bug to overwrite the variable runner, create a new variable called backTracked.
C# practice
August 8, 2016
Work on facebook code lab, preorder iterative solution. Chicken out! Forget to enforce the rule, right child goes to stack first, and then, left child goes to stack afterwards.
Need more practice!
3 smart choices to ask myself:
stack vs queue ->
left, right who goes first ->
enforce rule, null pointer will not go into stack, save time ->
one node ->
3 node tree - complete binary tree ->
7 node tree - complete binary tree
No comments:
Post a Comment