Skip to content

Commit 655a5ca

Browse files
committed
Two-Hundred-Fifty-Eight Commit: Add Same Tree problem to Concurrency section
1 parent d28327b commit 655a5ca

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Concurrency;
2+
3+
// Problem Statement: Same Tree (medium)
4+
// LeetCode Question: 100. Same Tree
5+
6+
public class Problem_1_Same_Tree {
7+
8+
class TreeNode {
9+
int val;
10+
TreeNode left;
11+
TreeNode right;
12+
public TreeNode(int val) {
13+
this.val = val;
14+
this.left = null;
15+
this.right = null;
16+
}
17+
}
18+
19+
public boolean isSameTree(TreeNode p, TreeNode q) {
20+
// p and q are both null
21+
if (p == null && q == null) return true;
22+
// one of p and q is null
23+
if (q == null || p == null) return false;
24+
// one of p and q has different value
25+
if (p.val != q.val) return false;
26+
27+
// check left and right subtree recursively
28+
return isSameTree(p.right, q.right) &&
29+
isSameTree(p.left, q.left);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)