File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments