-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101.对称二叉树.js
77 lines (75 loc) · 1.5 KB
/
101.对称二叉树.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* @lc app=leetcode.cn id=101 lang=javascript
*
* [101] 对称二叉树
*
* https://leetcode.cn/problems/symmetric-tree/description/
*
* algorithms
* Easy (58.30%)
* Likes: 2137
* Dislikes: 0
* Total Accepted: 703K
* Total Submissions: 1.2M
* Testcase Example: '[1,2,2,3,4,4,3]'
*
* 给你一个二叉树的根节点 root , 检查它是否轴对称。
*
*
*
* 示例 1:
*
*
* 输入:root = [1,2,2,3,4,4,3]
* 输出:true
*
*
* 示例 2:
*
*
* 输入:root = [1,2,2,null,3,null,3]
* 输出:false
*
*
*
*
* 提示:
*
*
* 树中节点数目在范围 [1, 1000] 内
* -100 <= Node.val <= 100
*
*
*
*
* 进阶:你可以运用递归和迭代两种方法解决这个问题吗?
*
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function (root) {
if (root === null) return true
return isMirror(root.left, root.right)
}
function isMirror(lRoot, rRoot) {
// null
if (lRoot === rRoot) return true
return (
Boolean(lRoot && rRoot) &&
lRoot.val === rRoot.val &&
isMirror(lRoot.left, rRoot.right) &&
isMirror(lRoot.right, rRoot.left)
)
}
// @lc code=end