File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ https://leetcode.com/problems/same-tree/description/
4
+ Given two binary trees, write a function to check if they are the same or not.
5
+
6
+ Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
7
+
8
+ Example 1:
9
+
10
+ Input: 1 1
11
+ / \ / \
12
+ 2 3 2 3
13
+
14
+ [1,2,3], [1,2,3]
15
+
16
+ Output: true
17
+ Example 2:
18
+
19
+ Input: 1 1
20
+ / \
21
+ 2 2
22
+
23
+ [1,2], [1,null,2]
24
+
25
+ Output: false
26
+ Example 3:
27
+
28
+ Input: 1 1
29
+ / \ / \
30
+ 2 1 1 2
31
+
32
+ [1,2,1], [1,1,2]
33
+
34
+ Output: false
35
+ */
36
+
37
+ /**
38
+ * Definition for a binary tree node.
39
+ * function TreeNode(val) {
40
+ * this.val = val;
41
+ * this.left = this.right = null;
42
+ * }
43
+ */
44
+ /**
45
+ * @param {TreeNode } p
46
+ * @param {TreeNode } q
47
+ * @return {boolean }
48
+ */
49
+ var isSameTree = function ( p , q ) {
50
+ if ( p === null )
51
+ return q === null ;
52
+
53
+ if ( q === null || p . val !== q . val )
54
+ return false ;
55
+
56
+ return isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right ) ;
57
+ } ;
You can’t perform that action at this time.
0 commit comments