-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q28_SymmeticTree.java
49 lines (40 loc) · 1 KB
/
Q28_SymmeticTree.java
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
package jianzhi_offer;
/**
* @author weiyuwang
* @since 2019/1/29 23:03
*/
public class Q28_SymmeticTree {
/**
* LeetCode 28
* 给定一个二叉树,检查它是否是镜像对称的。
* @param root
* @return
*/
public boolean isSymmetric(TreeNode root) {
if(root == null){
return true;
}
if(root.left == null && root.right == null){
return true;
}
if(root.left == null || root.right == null){
return false;
}
if(serial(root).equals(serial1(root))){
return true;
}
return false;
}
public String serial(TreeNode root){
if(root == null){
return "#";
}
return root.val + "_" + serial(root.left) + "_" + serial(root.right);
}
public String serial1(TreeNode root){
if(root == null){
return "#";
}
return root.val + "_" + serial1(root.right) + "_" + serial1(root.left);
}
}