-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (69 loc) · 1.42 KB
/
index.js
File metadata and controls
70 lines (69 loc) · 1.42 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* @lc app=leetcode id=101 lang=javascript
*
* [101] Symmetric Tree
*
* https://leetcode.com/problems/symmetric-tree/description/
*
* algorithms
* Easy (42.50%)
* Total Accepted: 588.6K
* Total Submissions: 1.3M
* Testcase Example: '[1,2,2,3,4,4,3]'
*
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric
* around its center).
*
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
*
*
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
*
*
*
*
* But the following [1,2,2,null,3,null,3] is not:
*
*
* 1
* / \
* 2 2
* \ \
* 3 3
*
*
*
*
* Follow up: Solve it both recursively and iteratively.
*
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
const isSymmetricHelp = (root1, root2) => {
if (!root1 && !root2) return true;
if (!root1 || !root2) return false;
if (root1.val !== root2.val) return false;
return isSymmetricHelp(root1.left, root2.right) && isSymmetricHelp(root1.right, root2.left)
};
return isSymmetricHelp(root, root)
};
module.exports = {
id:'101',
title:'Symmetric Tree',
url:'https://leetcode.com/problems/symmetric-tree/description/',
difficulty:'Easy',
}