-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymmetricTree.js
More file actions
23 lines (22 loc) · 799 Bytes
/
symmetricTree.js
File metadata and controls
23 lines (22 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//////////////////////////////////////////////Symmetric Tree/////////////////////////////////////////////////////
// Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
// Example 1:
//
// Input: root = [1,2,2,3,4,4,3]
// Output: true
// Example 2:
//
// Input: root = [1,2,2,null,3,null,3]
// Output: false
const isSymmetric = function(root) {
if(!root) return true;
function checker(l,r){
if(!l && !r) return true;
else if(!l || !r) return false;
else if(l.val !== r.val ) return false;
return (checker(l.left, r.right) && checker(l.right, r.left));
}
return checker (root.left ,root.right)
}
// console.log(isSymmetric([1,2,2,3,4,4,3]));
// console.log(isSymmetric([1,2,2,null,3,null,3]));