-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_101_Symmetric_Tree.java
37 lines (33 loc) · 1.22 KB
/
leetcode_101_Symmetric_Tree.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
public class leetcode_101_Symmetric_Tree
{
public static 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;
}
}
static class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
return isSymmetric(root.left,root.right);
}
private boolean isSymmetric(TreeNode rootL,TreeNode rootR) {
if(rootL==null&&rootR==null) return true;
if((rootL!=null&&rootR==null)||(rootL==null&&rootR!=null)) return false;
if(rootL.val != rootR.val) return false;
return (isSymmetric(rootL.right,rootR.left)&&isSymmetric(rootL.left,rootR.right));
}
}
public static void main(String[] args)
{
TreeNode root = new TreeNode(1, new TreeNode(2, new TreeNode(3), new TreeNode(4)), new TreeNode(2, new TreeNode(4), new TreeNode(3)));
boolean result = new Solution().isSymmetric(root);
System.out.println(result);
}
}