Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9da65b7

Browse files
2 parents f7576b6 + fd3220c commit 9da65b7

File tree

126 files changed

+17214
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+17214
-0
lines changed

‎100SameTree.cs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace _100SameTree
8+
{
9+
public class TreeNode
10+
{
11+
public int val;
12+
13+
public TreeNode left;
14+
public TreeNode right;
15+
16+
public TreeNode(int v)
17+
{
18+
val = v;
19+
}
20+
}
21+
22+
class Program
23+
{
24+
static void Main(string[] args)
25+
{
26+
}
27+
28+
/*
29+
* Leetcode: 100 same tree
30+
*
31+
* Reference:
32+
* http://blog.csdn.net/linhuanmars/article/details/22839819
33+
*
34+
* Analysis from the above blog:
35+
* 这道题是树的题目,属于最基本的树遍历的问题。问题要求就是判断两个树是不是一样,
36+
* 基于先序,中序或者后序遍历都可以做完成,因为对遍历顺序没有要求。
37+
*
38+
* 这里我们主要考虑一下结束条件,如果两个结点都是null,也就是到头了,那么返回true。
39+
* 如果其中一个是null,说明在一棵树上结点到头,另一棵树结点还没结束,即树不相同,
40+
* 或者两个结点都非空,并且结点值不相同,返回false。
41+
*
42+
* 最后递归处理两个结点的左右子树,返回左右子树递归的与结果即可。
43+
* 这里使用的是先序遍历,算法的复杂度跟遍历是一致的,如果使用递归,时间复杂度是O(n),
44+
* 空间复杂度是O(logn)
45+
*
46+
* Julia's comment:
47+
* 1. online judge:
48+
* 54 / 54 test cases passed.
49+
Status: Accepted
50+
Runtime: 176 ms
51+
*/
52+
public static bool IsSameTree(TreeNode p, TreeNode q)
53+
{
54+
if (p == null && q == null) // both nodes reaches the end
55+
return true;
56+
57+
if (p == null || q == null) // only one of them reaches the end
58+
return false;
59+
60+
if (p.val != q.val) // compare the value
61+
return false;
62+
63+
return IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right); // two subtrees comparison
64+
}
65+
66+
/*
67+
* Reference:
68+
* http://www.cnblogs.com/lautsie/p/3247097.html
69+
*
70+
* julia's comment:
71+
* 1. online judge:
72+
* 54 / 54 test cases passed.
73+
Status: Accepted
74+
Runtime: 156 ms
75+
*
76+
*/
77+
public static bool IsSameTree_B(TreeNode p, TreeNode q)
78+
{
79+
if (p == null && q == null) return true;
80+
81+
if (p != null && q != null)
82+
{
83+
if (p.val == q.val &&
84+
IsSameTree_B(p.left, q.left) &&
85+
IsSameTree_B(p.right, q.right))
86+
return true;
87+
}
88+
89+
return false;
90+
}
91+
92+
/*
93+
* Reference:
94+
* http://www.cnblogs.com/lautsie/p/3247097.html
95+
* comment from the above blog:
96+
* 非递归版本,使用了Queue,有点类似BFS。顺便说下Java里面的Queue真难用,
97+
* 连个empty()都没有,要用LinkedList(继承于Queue)
98+
*
99+
* http://blog.csdn.net/sunbaigui/article/details/8981275
100+
* Analysis from the above blog:
101+
*
102+
* julia's comment:
103+
*
104+
* 1. Read through C# LinkedList methods
105+
* 2. Try to find analog of Java LinkedList poll method in C#:
106+
* First,
107+
* RemoveFirst()
108+
* 3. First time, use LinkedListNode class in C#
109+
* LinkedList
110+
* LinkedListNode
111+
* understand why there are two classes, difference from Java - later.
112+
* 4. Java LinkedList offer - add the tail of list vs C# LinkedList AddLast
113+
* 5. online judge:
114+
* 54 / 54 test cases passed.
115+
Status: Accepted
116+
Runtime: 168 ms
117+
* 6. The tree traversal is kind of level traversal, BFS, two trees are compared
118+
* to level by level;
119+
* Julia argues that DFS search traversal does not work, counter example:
120+
* two tree can be different, but in-order traversal can be the same.
121+
* 2 3
122+
* / \ / \
123+
* 1 4 <- in order traversal both 1 2 3 4 - > 2 4
124+
* / /
125+
* 3 1
126+
*/
127+
public static bool isSameTree(TreeNode p, TreeNode q)
128+
{
129+
LinkedList<TreeNode> left = new LinkedList<TreeNode>();
130+
LinkedList<TreeNode> right = new LinkedList<TreeNode>();
131+
132+
left.AddFirst(p); // java LinkedList offer vs C# LinkedList AddFirst ?
133+
right.AddFirst(q);
134+
135+
while (left.Count > 0 && right.Count > 0)
136+
{
137+
LinkedListNode<TreeNode> l_f = left.First;
138+
LinkedListNode<TreeNode> r_f = right.First;
139+
140+
left.RemoveFirst();
141+
right.RemoveFirst();
142+
143+
TreeNode ln = l_f.Value;
144+
TreeNode rn = r_f.Value;
145+
146+
if (ln == null && rn == null)
147+
continue;
148+
149+
if (ln == null || rn == null)
150+
return false;
151+
152+
if (ln.val != rn.val)
153+
return false;
154+
155+
left.AddLast(ln.left);
156+
left.AddLast(ln.right);
157+
158+
right.AddLast(rn.left);
159+
right.AddLast(rn.right);
160+
}
161+
162+
if (left.Count > 0 || right.Count > 0)
163+
return false;
164+
165+
return true;
166+
}
167+
}
168+
}

‎101SymmetricTreeA.cs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace _101SymmetricTreeA
9+
{
10+
public class TreeNode
11+
{
12+
public int val;
13+
14+
public TreeNode left;
15+
public TreeNode right;
16+
17+
public TreeNode(int v)
18+
{
19+
val = v;
20+
}
21+
}
22+
23+
class Program
24+
{
25+
static void Main(string[] args)
26+
{
27+
}
28+
29+
/*
30+
* Reference:
31+
* http://www.cnblogs.com/TenosDoIt/p/3440729.html
32+
*
33+
* Analysis from the above blog:
34+
* 算法1:递归解法,判断左右两颗子树是否对称,只要两颗子树的根节点值相同,
35+
* 并且左边子树的左子树和右边子树的右子树对称 且左边子树的右子树和右边子
36+
* 树的左子树对称
37+
*
38+
* julia's comment:
39+
* 1. online judge:
40+
* 192 / 192 test cases passed.
41+
Status: Accepted
42+
Runtime: 176 ms
43+
*/
44+
public static bool isSymmetric(TreeNode root)
45+
{
46+
if (root == null)
47+
return true;
48+
49+
return isSymmetricRecur(root.left, root.right);
50+
}
51+
52+
/**
53+
*
54+
*/
55+
public static bool isSymmetricRecur(TreeNode r1, TreeNode r2)
56+
{
57+
if (r1 != null && r2 != null) // neither of two nodes is null
58+
{
59+
if (r1.val == r2.val &&
60+
isSymmetricRecur(r1.left, r2.right) &&
61+
isSymmetricRecur(r1.right, r2.left))
62+
return true;
63+
else
64+
return false;
65+
66+
}
67+
else if (r1 != null || r2 != null) // one of them is not null, but at least one is null
68+
return false;
69+
else // two of them are null
70+
return true;
71+
72+
}
73+
74+
75+
/*
76+
* code reference:
77+
* http://www.cnblogs.com/TenosDoIt/p/3440729.html
78+
* Iterative solution:
79+
算法2:非递归解法,用两个队列分别保存左子树节点和右子树节点,每次从两个队列中
80+
* 分别取出元素,如果两个元素的值相等,则继续把他们的左右节点加入左右队列。要注意
81+
* 每次取出的两个元素,左队列元素的左孩子要和右队列元素的右孩子要同时不为空或者
82+
* 同时为空,否则不可能对称,同理左队列元素的右孩子要和右队列元素的左孩子也一样。
83+
*
84+
* julia's comment:
85+
* 1. online judge:
86+
192 / 192 test cases passed.
87+
Status: Accepted
88+
Runtime: 156 ms
89+
* 2. The code can be improved by removing duplicated checking
90+
* 1st node'left child vs 2nd node's right child
91+
* 1st node'right child vs 2nd node's right child
92+
*
93+
* The rewrite version is called isSymmetric_Iterative_ShortVersion
94+
*/
95+
public static bool isSymmetric_Iterative(TreeNode root)
96+
{
97+
if (root == null)
98+
return true;
99+
100+
Queue lQ = new Queue(),
101+
rQ = new Queue();
102+
103+
if (root.left != null)
104+
lQ.Enqueue(root.left);
105+
106+
if (root.right != null)
107+
rQ.Enqueue(root.right);
108+
109+
while (lQ.Count > 0 && rQ.Count > 0)
110+
{
111+
TreeNode l_nd = (TreeNode)lQ.Peek(); // l_nd: left queue node
112+
TreeNode r_nd = (TreeNode)rQ.Peek(); // r_nd: right queue node
113+
114+
lQ.Dequeue();
115+
rQ.Dequeue();
116+
117+
if (l_nd.val == r_nd.val)
118+
{
119+
// l_nd (node from 1st queue) left child vs right queue (node from 2nd queue) right child
120+
if (l_nd.left != null && r_nd.right != null) // neither is null
121+
{
122+
lQ.Enqueue(l_nd.left);
123+
rQ.Enqueue(r_nd.right);
124+
}
125+
else if (l_nd.left != null || r_nd.right != null) // one of them is null, another is not null
126+
return false;
127+
128+
// 1st queue's node's right child vs 2nd queue's node's left child
129+
if (l_nd.right != null && r_nd.left != null)
130+
{
131+
lQ.Enqueue(r_nd.left);
132+
rQ.Enqueue(l_nd.right);
133+
}
134+
else if (r_nd.left != null || l_nd.right != null)
135+
return false;
136+
}
137+
else return false;
138+
}
139+
140+
if (lQ.Count == 0 && rQ.Count == 0)
141+
return true;
142+
else
143+
return false;
144+
}
145+
146+
/*
147+
* julia's comment:
148+
* make iterative solution short
149+
* 1. abstract code using two nodes: n1, n2
150+
* 2. make a loop iteration two times.
151+
* 3. think about how to make code more simple, readable, maintainable.
152+
*
153+
*/
154+
public static bool isSymmetric_Iterative_ShortVersion(TreeNode root)
155+
{
156+
if (root == null)
157+
return true;
158+
159+
Queue lQ = new Queue(),
160+
rQ = new Queue();
161+
162+
if (root.left != null)
163+
lQ.Enqueue(root.left);
164+
165+
if (root.right != null)
166+
rQ.Enqueue(root.right);
167+
168+
while (lQ.Count > 0 && rQ.Count > 0)
169+
{
170+
TreeNode l_nd = (TreeNode)lQ.Peek(); // l_nd: left queue node
171+
TreeNode r_nd = (TreeNode)rQ.Peek(); // r_nd: right queue node
172+
173+
lQ.Dequeue();
174+
rQ.Dequeue();
175+
176+
if (l_nd.val == r_nd.val)
177+
{
178+
for (int i = 0; i < 2; i++)
179+
{
180+
TreeNode n1 = l_nd.left;
181+
TreeNode n2 = r_nd.right;
182+
183+
if (i == 1)
184+
{
185+
n1 = l_nd.right;
186+
n2 = r_nd.left;
187+
}
188+
189+
if (n1 != null && n2 != null) // neither is null
190+
{
191+
lQ.Enqueue(n1);
192+
rQ.Enqueue(n2);
193+
}
194+
else if (n1 != null || n2 != null) // one of them is null, another is not null
195+
return false;
196+
}
197+
}
198+
else
199+
return false;
200+
}
201+
202+
if (lQ.Count == 0 && rQ.Count == 0) // both two queues are empty
203+
return true;
204+
else
205+
return false;
206+
}
207+
}
208+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /