forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
42 lines (37 loc) · 901 Bytes
/
Solution.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
/**
* @author Anonymous
* @since 2019/11/22
*/
/*
* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right =
* null;
*
* public TreeNode(int val) { this.val = val;
*
* }
*
* }
*/
public class Solution {
/**
* 判断是否是对称二叉树
*
* @param pRoot 二叉树的根结点
* @return 是否为对称二叉树
*/
boolean isSymmetrical(TreeNode pRoot) {
return isSymmetrical(pRoot, pRoot);
}
private boolean isSymmetrical(TreeNode pRoot1, TreeNode pRoot2) {
if (pRoot1 == null && pRoot2 == null) {
return true;
}
if (pRoot1 == null || pRoot2 == null) {
return false;
}
if (pRoot1.val != pRoot2.val) {
return false;
}
return isSymmetrical(pRoot1.left, pRoot2.right) && isSymmetrical(pRoot1.right, pRoot2.left);
}
}