-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0100. Same Tree.java
More file actions
32 lines (29 loc) · 879 Bytes
/
0100. Same Tree.java
File metadata and controls
32 lines (29 loc) · 879 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
/**
* 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 isSameTree(TreeNode p, TreeNode q) {
// checking if both of the TreeNodes are null.
if(p == null && q == null){
return true;
}
// if any one of the node is null or the value values are different.
if(p == null || q == null || p.val != q.val){
return false;
}
// checking for the left and right subtrees simultaneously.
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}