Skip to content

Commit

Permalink
🚧 更新文章
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinaCarlos committed Dec 13, 2024
1 parent 5694e82 commit cd037ef
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions docs/interview/Algorithm/writeAlgorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -1267,31 +1267,6 @@ function maxDepth(root) {
}
```
## 37. 二叉树的直径
```javascript
function diameterOfBinaryTree(root) {
let maxDiameter = 0; // 用于保存最大直径

// 递归计算树的深度并更新最大直径
function depth(node) {
if (!node) return 0; // 空节点深度为0

const leftDepth = depth(node.left); // 左子树的深度
const rightDepth = depth(node.right); // 右子树的深度

// 更新最大直径:当前节点的直径为左子树深度 + 右子树深度
maxDiameter = Math.max(maxDiameter, leftDepth + rightDepth);

// 当前节点的深度是左子树和右子树深度的最大值加1
return Math.max(leftDepth, rightDepth) + 1;
}

depth(root); // 从根节点开始递归计算
return maxDiameter; // 返回最终的最大直径
}
```
## 38. 最长连续序列
```javascript
Expand Down Expand Up @@ -1379,3 +1354,28 @@ function restoreIpAddresses(s) {
return result;
}
```
## 41. 二叉树的直径
```javascript
function diameterOfBinaryTree(root) {
let maxDiameter = 0; // 用于保存最大直径

// 递归计算树的深度并更新最大直径
function depth(node) {
if (!node) return 0; // 空节点深度为0

const leftDepth = depth(node.left); // 左子树的深度
const rightDepth = depth(node.right); // 右子树的深度

// 更新最大直径:当前节点的直径为左子树深度 + 右子树深度
maxDiameter = Math.max(maxDiameter, leftDepth + rightDepth);

// 当前节点的深度是左子树和右子树深度的最大值加1
return Math.max(leftDepth, rightDepth) + 1;
}

depth(root); // 从根节点开始递归计算
return maxDiameter; // 返回最终的最大直径
}
```

0 comments on commit cd037ef

Please sign in to comment.