Skip to content

Commit 7742c92

Browse files
committedJan 15, 2022
Add solution #100
1 parent b8d7f5c commit 7742c92

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
6161
94|[Binary Tree Inorder Traversal](./0094-binary-tree-inorder-traversal.js)|Easy|
6262
98|[Validate Binary Search Tree](./0098-validate-binary-search-tree.js)|Medium|
63+
100|[Same Tree](./0100-same-tree.js)|Easy|
6364
101|[Symmetric Tree](./0101-symmetric-tree.js)|Easy|
6465
102|[Binary Tree Level Order Traversal](./0102-binary-tree-level-order-traversal.js)|Medium|
6566
104|[Maximum Depth of Binary Tree](./0104-maximum-depth-of-binary-tree.js)|Easy|

‎solutions/0100-same-tree.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 100. Same Tree
3+
* https://leetcode.com/problems/same-tree/
4+
* Difficulty: Easy
5+
*
6+
* Given the roots of two binary trees p and q, write a function to check if they are
7+
* the same or not.
8+
*
9+
* Two binary trees are considered the same if they are structurally identical, and
10+
* the nodes have the same value.
11+
*/
12+
13+
/**
14+
* Definition for a binary tree node.
15+
* function TreeNode(val, left, right) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.left = (left===undefined ? null : left)
18+
* this.right = (right===undefined ? null : right)
19+
* }
20+
*/
21+
/**
22+
* @param {TreeNode} p
23+
* @param {TreeNode} q
24+
* @return {boolean}
25+
*/
26+
var isSameTree = function(p, q) {
27+
const hasSameValue = p !== null && q !== null && p.val === q.val;
28+
const hasSameTree = hasSameValue && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
29+
return (p === null && q === null) || hasSameTree;
30+
};

0 commit comments

Comments
 (0)
Please sign in to comment.