-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100. Same Tree.java
39 lines (35 loc) · 1.03 KB
/
100. Same 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
38
39
/**
* 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 {
boolean is_simetric = true;
public void preorder(TreeNode p, TreeNode q) {
// Function to perform preorder tranversal
// Check if p and q are not null
if (p != null && q != null) {
if (p.val != q.val)
is_simetric = is_simetric && false;
preorder(p.left, q.left);
preorder(p.right, q.right);
}
// If one of p or q is null
else if ((p == null && q != null) || (p != null && q == null))
is_simetric = is_simetric && false;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
preorder(p, q);
return is_simetric;
}
}