-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path124BTreeLevelOrderTraversal.cs
193 lines (168 loc) · 6.71 KB
/
124BTreeLevelOrderTraversal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BTreeLevelOrderTraversal
{
/*
* julia's comment (July 23, 2015):
* 1. c++ structure everything default is public, but class member is private; so add public
* 2. keep structure in C# from c++, trouble to compare to null value; and then,
* change to class.
*/
class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) {
val = x;
left = null;
right = null;
}
};
class BTreeLevelOrderTraversal
{
const Int16 NULL_INT = 0;
static void Main(string[] args)
{
/*Latest update: July 23, 2015
* Test the code and then find a bug
*/
TreeNode n1 = new TreeNode(1);
n1.left = new TreeNode(2);
n1.right = new TreeNode(3);
n1.left.left = new TreeNode(4);
n1.left.right = new TreeNode(5);
n1.right.left = new TreeNode(6);
n1.right.right = new TreeNode(7);
ArrayList result = levelOrder_bugVersion(n1);
/*
* Try to fix the bug
*
*/
ArrayList result2 = levelOrder_bugFree(n1);
}
/**
* source code from blog:
* https://github.com/xiaoxq/leetcode-cpp/blob/master/src/BinaryTreeLevelOrderTraversal.cpp
* convert from C++ to C#
* julia's comment:
* 1. check leetcode online judge, need to use IList<IList<int>>
* 2. how to use queue for level order traversal, null point as level divider. Kind of tricky.
* first action before the loop, push first node into the queue, and then
* push level divider: null point
* design null pointer action: once null pointer is met, and then,
* finish the current level, go to next level:
* 1. first, save the current level visited value into the result for return;
* 2. second, check if the queue is empty -> yes, finish last level, ready to exit.
* 3. third, prepare for next level:
* A: clear the container for current level info
* B: review facts: B.1. there is no null in the queue
* B.2. there is only one level nodes in the queue, will be current level
* So, need to add level divider now.
* 3. C# has a bug - share one arrayList object for each level,
* use clear method will not work out.
* lesson A: C# List.addAll <-> C# ArrayList.addRange
*/
public static ArrayList levelOrder_bugVersion(TreeNode root)
{
ArrayList result = new ArrayList();
if( root == null)
return result;
ArrayList level = new ArrayList();
Queue<TreeNode> toVisit = new Queue<TreeNode>();
toVisit.Enqueue( root );
toVisit.Enqueue( null );
while( true )
{
TreeNode cur = toVisit.Peek();
toVisit.Dequeue();
// end a level
if( cur==null )
{
result.Add( level );
if( toVisit.Count == 0 )
break;
// add a new End-Of-Level
level.Clear(); // julia's comment: C# bug, cannot share one object -
//need to create new object for each level
toVisit.Enqueue( null );
}
else
{
level.Add( cur.val );
if( cur.left != null)
toVisit.Enqueue( cur.left );
if( cur.right != null )
toVisit.Enqueue( cur.right );
}
}
return result;
}
/**
* source code from blog:
* https://github.com/xiaoxq/leetcode-cpp/blob/master/src/BinaryTreeLevelOrderTraversal.cpp
* convert from C++ to C#
* julia's comment:
* 1. check leetcode online judge, need to use IList<IList<int>>
* 2. how to use queue for level order traversal, null point as level divider. Kind of tricky.
* first action before the loop, push first node into the queue, and then
* push level divider: null point
* design null pointer action: once null pointer is met, and then,
* finish the current level, go to next level:
* 1. first, save the current level visited value into the result for return;
* 2. second, check if the queue is empty -> yes, finish last level, ready to exit.
* 3. third, prepare for next level:
* A: clear the container for current level info
* B: review facts: B.1. there is no null in the queue
* B.2. there is only one level nodes in the queue, will be current level
* So, need to add level divider now.
* 3. C# has a bug - share one arrayList object for each level,
* use clear method will not work out.
* 4. Fix the bug -
* First try failed, using " copy the list, same pointer"
second try, use addRange method, work out perfect!
* http://stackoverflow.com/questions/10022441/how-to-move-contents-of-one-arraylist-to-another
* C# List.addAll <-> C# ArrayList.addRange method
*/
public static ArrayList levelOrder_bugFree(TreeNode root)
{
ArrayList result = new ArrayList();
if (root == null)
return result;
ArrayList level = new ArrayList();
Queue<TreeNode> toVisit = new Queue<TreeNode>();
toVisit.Enqueue(root);
toVisit.Enqueue(null);
while (true)
{
TreeNode cur = toVisit.Peek();
toVisit.Dequeue();
// end a level
if (cur == null)
{
ArrayList list = new ArrayList(); // julia: bug fix
list.AddRange(level); // julia: bug fix
result.Add(list);
if (toVisit.Count == 0)
break;
// add a new End-Of-Level
level.Clear();
toVisit.Enqueue(null);
}
else
{
level.Add(cur.val);
if (cur.left != null)
toVisit.Enqueue(cur.left);
if (cur.right != null)
toVisit.Enqueue(cur.right);
}
}
return result;
}
}
}