-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathstupid-solution.js
61 lines (47 loc) · 1.09 KB
/
stupid-solution.js
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
// Source : https://leetcode.com/problems/symmetric-tree/
// Author : Han Zichi
// Date : 2016-08-05
/**
* 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) {
if (!root)
return true;
// left[n] 代表二叉树左子树从左到右的 value 数组
var left = [];
// right[n] 代表二叉树右子树从左到右的 value 数组
var right = [];
dfs(root.left, 1, left);
dfs(root.right, 1, right);
if (left.length !== right.length)
return false;
for (var i = 1, len = left.length; i < len; i++) {
var a = left[i];
var b = right[i];
b.reverse();
if (a.length !== b.length)
return false;
if (a.join('|') !== b.join('|'))
return false;
}
return true;
};
function dfs(node, step, arr) {
if (!arr[step])
arr[step] = [];
if (!node) {
arr[step].push(null);
return;
}
arr[step].push(node.val);
dfs(node.left, step + 1, arr);
dfs(node.right, step + 1, arr);
}