-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_101.java
More file actions
32 lines (27 loc) · 877 Bytes
/
Solution_101.java
File metadata and controls
32 lines (27 loc) · 877 Bytes
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
package com.leetcode.algorithm.binarytree;
import com.leetcode.algorithm.TreeNode;
public class Solution_101 {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return compareTowNode(root.left, root.right);
}
/**
* 比较两个节点的值,不要跳进递归的细节中
* 该方法只关注两个节点的值,剩下的交给递归来做
* @param node1
* @param node2
* @return
*/
public boolean compareTowNode(TreeNode node1, TreeNode node2) {
if (node1 == null && node2 == null) {
return true;
}
if (node1 == null || node2 == null) {
return false;
}
return node1.val == node2.val && compareTowNode(node1.left, node2.right)
&& compareTowNode(node1.right, node2.left);
}
}