Skip to content

Commit d379ace

Browse files
committed
feat: hand practice
1 parent 1d968de commit d379ace

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

demos/leetcode/binaryTree.html

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
67
<title>binaryTree - 二叉树</title>
78
</head>
9+
810
<body>
911
<script>
1012
// Todo: 二叉树相关
@@ -133,8 +135,10 @@
133135
// 2. 每个树的右子树都与另一个树的左子树镜像对称
134136

135137
// function isSymmetric(root) {
136-
// return isMirror(root, root)
138+
// if (!root) return true; // 空树是对称的
139+
// return isMirror(root.left, root.right);
137140
// }
141+
138142
// function isMirror(root1, root2) {
139143
// if (root1 == null && root2 == null) {
140144
// return true
@@ -147,6 +151,50 @@
147151
// && isMirror(root2.left, root1.right)
148152
// }
149153

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)))
150198

151199

152200

@@ -157,4 +205,5 @@
157205

158206
</script>
159207
</body>
208+
160209
</html>

0 commit comments

Comments
 (0)