File tree Expand file tree Collapse file tree 1 file changed +4
-0
lines changed Expand file tree Collapse file tree 1 file changed +4
-0
lines changed Original file line number Diff line number Diff line change @@ -23,17 +23,21 @@ List is periodically updated
23
23
const isSymmetric = function(root) {
24
24
// tree with null root considered symmetric
25
25
if (root == null) return true
26
+
26
27
// check if same first two nodes of the root
27
28
return isSame(root.left, root.right)
28
29
};
29
30
30
31
const isSame = (leftN, rightN) => {
31
32
// if both nodes are null then tree symmetric
32
33
if (leftN == null && rightN == null) return true
34
+
33
35
// if only one of them null, no longer symmetric
34
36
if (leftN == null || rightN == null) return false
37
+
35
38
// if two values are not equal , not symmetric
36
39
if (leftN.val !== rightN.val) return false
40
+
37
41
// recursivly check both outer sides of the seconf node and inner sides respectively all over the height of the tree
38
42
return isSame(leftN.left, rightN.right) && isSame(leftN.right, rightN.left);
39
43
}
You can’t perform that action at this time.
0 commit comments