File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ Date: Dec 25, 2014
4
+ Problem: Same Tree
5
+ Difficulty: easy
6
+ Source: http://leetcode.com/onlinejudge#question_100
7
+ Notes:
8
+ Given two binary trees, write a function to check if they are equal or not.
9
+ Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
10
+
11
+ Solution: recursion.
12
+ */
13
+ /**
14
+ * Definition for binary tree
15
+ * public class TreeNode {
16
+ * int val;
17
+ * TreeNode left;
18
+ * TreeNode right;
19
+ * TreeNode(int x) { val = x; }
20
+ * }
21
+ */
22
+ public class Solution {
23
+ public boolean isSameTree (TreeNode p , TreeNode q ) {
24
+ if (p == null && q == null ) return true ;
25
+ if ((p != null && q == null ) || (p == null && q != null )) return false ;
26
+ if (p .val != q .val ) return false ;
27
+ return isSameTree (p .left , q .left ) && isSameTree (p .right , q .right );
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments