-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path101SymmetricTreeA.cs
208 lines (179 loc) · 6.77 KB
/
101SymmetricTreeA.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _101SymmetricTreeA
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int v)
{
val = v;
}
}
class Program
{
static void Main(string[] args)
{
}
/*
* Reference:
* http://www.cnblogs.com/TenosDoIt/p/3440729.html
*
* Analysis from the above blog:
* 算法1:递归解法,判断左右两颗子树是否对称,只要两颗子树的根节点值相同,
* 并且左边子树的左子树和右边子树的右子树对称 且左边子树的右子树和右边子
* 树的左子树对称
*
* julia's comment:
* 1. online judge:
* 192 / 192 test cases passed.
Status: Accepted
Runtime: 176 ms
*/
public static bool isSymmetric(TreeNode root)
{
if (root == null)
return true;
return isSymmetricRecur(root.left, root.right);
}
/**
*
*/
public static bool isSymmetricRecur(TreeNode r1, TreeNode r2)
{
if (r1 != null && r2 != null) // neither of two nodes is null
{
if (r1.val == r2.val &&
isSymmetricRecur(r1.left, r2.right) &&
isSymmetricRecur(r1.right, r2.left))
return true;
else
return false;
}
else if (r1 != null || r2 != null) // one of them is not null, but at least one is null
return false;
else // two of them are null
return true;
}
/*
* code reference:
* http://www.cnblogs.com/TenosDoIt/p/3440729.html
* Iterative solution:
算法2:非递归解法,用两个队列分别保存左子树节点和右子树节点,每次从两个队列中
* 分别取出元素,如果两个元素的值相等,则继续把他们的左右节点加入左右队列。要注意
* 每次取出的两个元素,左队列元素的左孩子要和右队列元素的右孩子要同时不为空或者
* 同时为空,否则不可能对称,同理左队列元素的右孩子要和右队列元素的左孩子也一样。
*
* julia's comment:
* 1. online judge:
192 / 192 test cases passed.
Status: Accepted
Runtime: 156 ms
* 2. The code can be improved by removing duplicated checking
* 1st node'left child vs 2nd node's right child
* 1st node'right child vs 2nd node's right child
*
* The rewrite version is called isSymmetric_Iterative_ShortVersion
*/
public static bool isSymmetric_Iterative(TreeNode root)
{
if (root == null)
return true;
Queue lQ = new Queue(),
rQ = new Queue();
if (root.left != null)
lQ.Enqueue(root.left);
if (root.right != null)
rQ.Enqueue(root.right);
while (lQ.Count > 0 && rQ.Count > 0)
{
TreeNode l_nd = (TreeNode)lQ.Peek(); // l_nd: left queue node
TreeNode r_nd = (TreeNode)rQ.Peek(); // r_nd: right queue node
lQ.Dequeue();
rQ.Dequeue();
if (l_nd.val == r_nd.val)
{
// l_nd (node from 1st queue) left child vs right queue (node from 2nd queue) right child
if (l_nd.left != null && r_nd.right != null) // neither is null
{
lQ.Enqueue(l_nd.left);
rQ.Enqueue(r_nd.right);
}
else if (l_nd.left != null || r_nd.right != null) // one of them is null, another is not null
return false;
// 1st queue's node's right child vs 2nd queue's node's left child
if (l_nd.right != null && r_nd.left != null)
{
lQ.Enqueue(r_nd.left);
rQ.Enqueue(l_nd.right);
}
else if (r_nd.left != null || l_nd.right != null)
return false;
}
else return false;
}
if (lQ.Count == 0 && rQ.Count == 0)
return true;
else
return false;
}
/*
* julia's comment:
* make iterative solution short
* 1. abstract code using two nodes: n1, n2
* 2. make a loop iteration two times.
* 3. think about how to make code more simple, readable, maintainable.
*
*/
public static bool isSymmetric_Iterative_ShortVersion(TreeNode root)
{
if (root == null)
return true;
Queue lQ = new Queue(),
rQ = new Queue();
if (root.left != null)
lQ.Enqueue(root.left);
if (root.right != null)
rQ.Enqueue(root.right);
while (lQ.Count > 0 && rQ.Count > 0)
{
TreeNode l_nd = (TreeNode)lQ.Peek(); // l_nd: left queue node
TreeNode r_nd = (TreeNode)rQ.Peek(); // r_nd: right queue node
lQ.Dequeue();
rQ.Dequeue();
if (l_nd.val == r_nd.val)
{
for (int i = 0; i < 2; i++)
{
TreeNode n1 = l_nd.left;
TreeNode n2 = r_nd.right;
if (i == 1)
{
n1 = l_nd.right;
n2 = r_nd.left;
}
if (n1 != null && n2 != null) // neither is null
{
lQ.Enqueue(n1);
rQ.Enqueue(n2);
}
else if (n1 != null || n2 != null) // one of them is null, another is not null
return false;
}
}
else
return false;
}
if (lQ.Count == 0 && rQ.Count == 0) // both two queues are empty
return true;
else
return false;
}
}
}