Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 860e453

Browse files
committed
review some code
1 parent c63c731 commit 860e453

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

same-tree.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
2424
* @param {TreeNode} q
2525
* @returns {boolean}
2626
*/
27+
/**
28+
* Memo: compare root and children recursively.
29+
* Complex: O(nlgn)
30+
* Runtime: ms
31+
* Tests: 54 test cases passed
32+
* Rank: S
33+
*/
34+
var isSameTree = function(p, q) {
35+
if ((!p && q) || (p && !q)) return false; // one is null
36+
if (!p && !q) return true; // cautious both null nodes generates true
37+
if (p.val !== q.val) return false; // val not equal
38+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); // check children
39+
};
40+
2741
var isSameTree = function(p, q) {
2842
if ((p && !q) || (!p && q)) {
2943
return false;
@@ -34,7 +48,9 @@ var isSameTree = function(p, q) {
3448

3549
if (p.val !== q.val) {
3650
return false;
37-
}else{
51+
} else {
3852
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
3953
}
4054
};
55+
56+
console.log(isSameTree(null,null));

0 commit comments

Comments
 (0)