Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 17b5bd5

Browse files
Create Same_Tree.js
1 parent ed111d6 commit 17b5bd5

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

LeetcodeProblems/Same_Tree.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
};

0 commit comments

Comments
 (0)