Skip to content

Commit 96aee98

Browse files
author
Vamshi Bachaneboina
committed
patch for 100 - dfs base case checks
1 parent 9d668c6 commit 96aee98

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

javascript/0100-same-tree.js

+39-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
/**
2-
* https://leetcode.com/problems/same-tree/
3-
* TIme O(N) | Space O(H)
2+
* Check if both nodes are null (end of a branch in both trees)
43
* @param {TreeNode} p
54
* @param {TreeNode} q
65
* @return {boolean}
76
*/
8-
var isSameTree = function(p, q) {
9-
const isBaseCase = !(p || q);
10-
if (isBaseCase) return true;
7+
var isSameTree = function (p, q) {
8+
// Check if both nodes are null (end of a branch in both trees)
9+
const areBothNodesNull = p == null && q == null;
10+
if (areBothNodesNull) return true;
1111

12-
const isBalanced = (p && q);
13-
if (!isBalanced) return false;
12+
// Check if only one node is null (mismatch in tree structure)
13+
const isOnlyOneNodeNull = p == null || q == null;
14+
if (isOnlyOneNodeNull) return false;
1415

15-
const isSame = p.val === q.val;
16-
if (!isSame) return false;
16+
// Check if node values are equal (mismatch in node values)
17+
const doNodesHaveEqualValue = p.val == q.val;
18+
if (!doNodesHaveEqualValue) return false;
1719

20+
// Recursively check left and right subtrees
1821
return dfs(p, q);
1922
};
2023

24+
/**
25+
* * https://leetcode.com/problems/same-tree/
26+
* * Time complexity is O(N), where N is the total number of nodes in the tree.
27+
* This is because in the worst-case scenario, we need to visit every node once.
28+
29+
* * Space complexity is O(H), where H is the height of the tree.
30+
* This is because in the worst-case scenario (a skewed tree), the maximum
31+
* amount of space is consumed by the recursive stack.
32+
* @param {*} p
33+
* @param {*} q
34+
* @returns
35+
*/
2136
const dfs = (p, q) => {
2237
const left = isSameTree(p.left, q.left);
2338
const right = isSameTree(p.right, q.right);
2439

2540
return left && right;
26-
}
41+
};
2742

2843
/**
2944
* https://leetcode.com/problems/same-tree/
@@ -32,40 +47,41 @@ const dfs = (p, q) => {
3247
* @param {TreeNode} q
3348
* @return {boolean}
3449
*/
35-
var isSameTree = function(p, q) {
50+
var isSameTree = function (p, q) {
3651
if (isSameNode(p, q)) return true;
3752

38-
return bfs([[ p, q ]]);
39-
}
53+
return bfs([[p, q]]);
54+
};
4055

4156
const bfs = (queue) => {
4257
while (queue.length) {
43-
for (let i = (queue.length - 1); 0 <= i; i--) {
44-
const [ p, q ] = queue.shift();
58+
for (let i = queue.length - 1; 0 <= i; i--) {
59+
const [p, q] = queue.shift();
4560

4661
if (!isSame(p, q)) return false;
4762

48-
if (p.left) queue.push([ p.left, q.left ]);
49-
if (p.right) queue.push([ p.right, q.right ]);
63+
if (p.left) queue.push([p.left, q.left]);
64+
if (p.right) queue.push([p.right, q.right]);
5065
}
5166
}
5267

5368
return true;
54-
}
69+
};
5570

5671
const isSameNode = (p, q) => {
5772
const isBaseCase = !(p || q);
5873
if (isBaseCase) return true;
5974

60-
const isBalanced = (p && q);
75+
const isBalanced = p && q;
6176
if (!isBalanced) return false;
6277

6378
const isSame = p.val === q.val;
6479
if (!isSame) return false;
6580

6681
return true;
67-
}
82+
};
6883

69-
const isSame = (p, q) => isSameNode(p, q)
70-
&& isSameNode(p.left, q.left)
71-
&& isSameNode(p.right, q.right);
84+
const isSame = (p, q) =>
85+
isSameNode(p, q) &&
86+
isSameNode(p.left, q.left) &&
87+
isSameNode(p.right, q.right);

0 commit comments

Comments
 (0)