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

Appearance settings

Latest commit

 

History

History
History
112 lines (98 loc) · 3.33 KB

File metadata and controls

112 lines (98 loc) · 3.33 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package Algorithms.tree;
import java.util.Stack;
public class IsSymmetric_LeetCode {
/*
* Given a binary tree, check whether it is a mirror of itself (ie,
* symmetric around its center).
*
* For example, this binary tree is symmetric:
*
* 1 / \ 2 2 / \ / \ 3 4 4 3
*
* But the following is not:
*
* 1 / \ 2 2 \ \ 3 3
*
* Note: Bonus points if you could solve it both recursively and
* iteratively.
*
* confused what "{1,#,2,3}" means? > read more on how binary tree is
* serialized on OJ.
*
* OJ's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where
* '#' signifies a path terminator where no node exists below.
*
* Here's an example:
*
* 1 / \ 2 3 / 4 \ 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*/
//
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return isMirrorRec(root.left, root.right);
}
/*
* 判断两个树是否互相镜像
* (1) 根必须同时为空,或是同时不为空
*
* 如果根不为空:
* (1).根的值一样
* (2).r1的左树是r2的右树的镜像
* (3).r1的右树是r2的左树的镜像
*/
public boolean isMirrorRec(TreeNode r1, TreeNode r2) {
if (r1 == null && r2 == null) {
return true;
}
if (r1 == null || r2 == null) {
return false;
}
// should compare the value of the root. remember this.
return r1.val == r2.val
&& isMirrorRec(r1.left, r2.right)
&& isMirrorRec(r1.right, r2.left);
}
public boolean isMirror(TreeNode r1, TreeNode r2) {
if (r1 == null && r2 == null) {
return true;
}
if (r1 == null || r2 == null) {
return false;
}
// We can do preOrder traverse to judge if the trees are mirror.
Stack<TreeNode> s1 = new Stack<TreeNode>();
Stack<TreeNode> s2 = new Stack<TreeNode>();
s1.push(r1);
s2.push(r2);
while (!s1.isEmpty() && !s2.isEmpty()) {
// pop the current node out.
TreeNode cur1 = s1.pop();
TreeNode cur2 = s2.pop();
// Judge if the value of the node is equal.
if (cur1.val != cur2.val) {
return false;
}
// tree1的左节点,tree2的右节点,可以同时不为空,也可以同时为空,否则返回false.
if (cur1.left != null && cur2.right != null) {
s1.push(cur1.left);
s2.push(cur2.right);
} else if (!(cur1.left == null && cur2.right == null)) {
return false;
}
// tree1的右节点,tree2的左节点,可以同时不为空,也可以同时为空,否则返回false.
if (cur1.right != null && cur2.left != null) {
s1.push(cur1.right);
s2.push(cur2.left);
} else if (!(cur1.right == null && cur2.left == null)) {
return false;
}
}
return true;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.