-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path101-SymmetricTree.java
More file actions
35 lines (32 loc) · 944 Bytes
/
101-SymmetricTree.java
File metadata and controls
35 lines (32 loc) · 944 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
//Question Link - https://leetcode.com/problems/symmetric-tree/
/**
* 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 sym(TreeNode root1,TreeNode root2){
//base case
if(root1==null || root2==null){
return root1==root2;
}
if(root1.val != root2.val){
return false;
}
//checking for the children node in left and right subtree parallely
return sym(root1.left, root2.right) && sym(root1.right, root2.left);
}
public boolean isSymmetric(TreeNode root) {
return sym(root.left, root.right);
}
}