-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsame-tree.js
More file actions
52 lines (48 loc) · 1.51 KB
/
same-tree.js
File metadata and controls
52 lines (48 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// https://leetcode.com/problems/same-tree/
// Related Topics: Tree, Depth-first Search
// Difficulty: Easy
/*
Initial thoughts:
For two trees to be the same, their structure and values must be the same.
Using a Depth-frist Search approach we are going to touch and compare each
node on both trees at the same time and return false if there is any
dissimilarity. If our algorithm reaches the end without encountering any
viariance between the nodes, we return true.
Time complexity: O(Min(n,m)) where n === number of nodes in p and m === number of nodes in q.
The reason is that if the trees are not the same, the number of the nodes could differ but our
algorithm will stop at most after visiting all the nodes of the smaller tree.
Space complexity: O(n) in the worst case when the trees are completely unbalanced and
O(log n) if they are completely balanced.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
const isSameTree = (p, q) => {
function check(p, q) {
// both leaf
if (!p && !q) return true;
// one leaf
if ((!p && q) || (p && !q)) return false;
if (p.val !== q.val) return false;
return true;
}
const queue = [[p, q]];
while (queue.length) {
const [p, q] = queue.shift();
if (!check(p, q)) return false;
if (p) {
queue.push([p.left, q.left]);
queue.push([p.right, q.right]);
}
}
return true;
};