forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymmetricTree.java
32 lines (25 loc) · 875 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
31
32
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;
}
}
public class SymmetricTree {
public boolean isSymmetric(TreeNode root) {
return isSymmetricHelper(root,root);
}
public boolean isSymmetricHelper(TreeNode lnode,TreeNode rnode){
if(lnode==null && rnode==null) return true;
if(lnode==null || rnode==null || lnode.val!=rnode.val) return false;
boolean b1=isSymmetricHelper(lnode.left,rnode.right);
if(b1==false || lnode==rnode) return b1;
boolean b2=isSymmetricHelper(lnode.right,rnode.left);
return b1 && b2;
}
}