mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
新增 117题、104题、111题等 JavaScript版本代码
新增 117.填充每个节点的下一个右侧节点指针II JavaScript版本解法代码 新增 104二叉树最大深度 JavaScript版本解法代码 新增 111.二叉树的最小深度 JavaScript版本解法代码
This commit is contained in:
@ -1426,7 +1426,39 @@ class Solution:
|
||||
first = dummyHead.next # 此处为换行操作,更新到下一行
|
||||
return root
|
||||
```
|
||||
JavaScript:
|
||||
```javascript
|
||||
/**
|
||||
* // Definition for a Node.
|
||||
* function Node(val, left, right, next) {
|
||||
* this.val = val === undefined ? null : val;
|
||||
* this.left = left === undefined ? null : left;
|
||||
* this.right = right === undefined ? null : right;
|
||||
* this.next = next === undefined ? null : next;
|
||||
* };
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Node} root
|
||||
* @return {Node}
|
||||
*/
|
||||
var connect = function(root) {
|
||||
if (root === null) {
|
||||
return null;
|
||||
}
|
||||
let queue = [root];
|
||||
while (queue.length > 0) {
|
||||
let n = queue.length;
|
||||
for (let i=0; i<n; i++) {
|
||||
let node = queue.shift();
|
||||
if (i < n-1) node.next = queue[0];
|
||||
if (node.left != null) queue.push(node.left);
|
||||
if (node.right != null) queue.push(node.right);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
};
|
||||
```
|
||||
go:
|
||||
|
||||
```GO
|
||||
@ -1608,6 +1640,36 @@ func maxDepth(root *TreeNode) int {
|
||||
|
||||
|
||||
JavaScript:
|
||||
```javascript
|
||||
/**
|
||||
* 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 {number}
|
||||
*/
|
||||
var maxDepth = function(root) {
|
||||
// 最大的深度就是二叉树的层数
|
||||
if (root === null) return 0;
|
||||
let queue = [root];
|
||||
let height = 0;
|
||||
while (queue.length) {
|
||||
let n = queue.length;
|
||||
height++;
|
||||
for (let i=0; i<n; i++) {
|
||||
let node = queue.shift();
|
||||
node.left && queue.push(node.left);
|
||||
node.right && queue.push(node.right);
|
||||
}
|
||||
}
|
||||
return height;
|
||||
};
|
||||
```
|
||||
|
||||
# 111.二叉树的最小深度
|
||||
|
||||
@ -1746,9 +1808,40 @@ func minDepth(root *TreeNode) int {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
JavaScript:
|
||||
```javascript
|
||||
/**
|
||||
* 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 {number}
|
||||
*/
|
||||
var minDepth = function(root) {
|
||||
if (root === null) return 0;
|
||||
let queue = [root];
|
||||
let deepth = 0;
|
||||
while (queue.length) {
|
||||
let n = queue.length;
|
||||
deepth++;
|
||||
for (let i=0; i<n; i++) {
|
||||
let node = queue.shift();
|
||||
// 如果左右节点都是null,则该节点深度最小
|
||||
if (node.left === null && node.right === null) {
|
||||
return deepth;
|
||||
}
|
||||
node.left && queue.push(node.left);;
|
||||
node.right && queue.push (node.right);
|
||||
}
|
||||
}
|
||||
return deepth;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user