1
1
<!DOCTYPE html>
2
2
< html lang ="en ">
3
+
3
4
< head >
4
5
< meta charset ="UTF-8 ">
5
6
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
7
< title > binaryTree - 二叉树</ title >
7
8
</ head >
9
+
8
10
< body >
9
11
< script >
10
12
// Todo: 二叉树相关
133
135
// 2. 每个树的右子树都与另一个树的左子树镜像对称
134
136
135
137
// function isSymmetric(root) {
136
- // return isMirror(root, root)
138
+ // if (!root) return true; // 空树是对称的
139
+ // return isMirror(root.left, root.right);
137
140
// }
141
+
138
142
// function isMirror(root1, root2) {
139
143
// if (root1 == null && root2 == null) {
140
144
// return true
147
151
// && isMirror(root2.left, root1.right)
148
152
// }
149
153
154
+ // Todo: 二叉树的最大深度
155
+ // https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/?envType=study-plan-v2&envId=top-100-liked
156
+ // 给定一个二叉树 root ,返回其最大深度。
157
+ // 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
158
+ // 示例 1:
159
+ // 输入:root = [3,9,20,null,null,15,7]
160
+ // 输出:3
161
+
162
+ // 示例 2:
163
+ // 输入:root = [1,null,2]
164
+ // 输出:2
165
+
166
+ // 核心思路:使用层序遍历,记录 deepth 就可以了?
167
+
168
+ // function solution(root) {
169
+ // let deepth = 0, queue = []
170
+ // if (!root) {
171
+ // return deepth
172
+ // }
173
+ // queue.push(root)
174
+ // while (queue.length !== 0) {
175
+ // let len = queue.length
176
+ // deepth++
177
+ // for (let i = 0; i < len; i++) {
178
+ // let node = queue.shift()
179
+ // node?.left && queue.push(node?.left)
180
+ // node?.right && queue.push(node?.right)
181
+ // }
182
+ // }
183
+ // return deepth
184
+ // }
185
+
186
+ // var maxDepth = function (root) {
187
+ // if (!root) {
188
+ // return 0;
189
+ // } else {
190
+ // const left = maxDepth(root.left);
191
+ // const right = maxDepth(root.right);
192
+ // return Math.max(left, right) + 1;
193
+ // }
194
+ // };
195
+
196
+ // // console.log(maxDepth(arrayToTree(arr)))
197
+ // console.log(maxDepth(arrayToTree(arr)))
150
198
151
199
152
200
157
205
158
206
</ script >
159
207
</ body >
208
+
160
209
</ html >
0 commit comments