-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymmetricTree.java
30 lines (28 loc) · 952 Bytes
/
SymmetricTree.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
// https://leetcode.com/problems/symmetric-tree/
// #tree #dfs #bfs
/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode
* right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left,
* TreeNode right) { this.val = val; this.left = left; this.right = right; } }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return check(root.left, root.right);
}
private boolean check(TreeNode nodeLeft, TreeNode nodeRight) {
if (nodeLeft == null && nodeRight == null) {
return true;
}
if ((nodeLeft == null && nodeRight != null) || (nodeLeft != null && nodeRight == null)) {
return false;
}
if (nodeLeft.val != nodeRight.val) {
return false;
}
boolean result1 = check(nodeLeft.left, nodeRight.right);
if (result1 == false) {
return false;
}
return check(nodeLeft.right, nodeRight.left);
}
}