forked from shijbian/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetricTree.java
More file actions
37 lines (28 loc) · 841 Bytes
/
SymmetricTree.java
File metadata and controls
37 lines (28 loc) · 841 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
33
34
35
36
37
package net.kenyang.algorithm;
public class SymmetricTree {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return isSymmetric(root.left, root.right);
}
public boolean isSymmetric(TreeNode leftNode, TreeNode rightNode) {
if (rightNode == null && leftNode == null)
return true;
if (rightNode == null)
return false;
if (leftNode == null)
return false;
return leftNode.val == rightNode.val
&& isSymmetric(leftNode.left, rightNode.right)
&& isSymmetric(leftNode.right, rightNode.left);
}
}