mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -341,7 +341,7 @@ class Solution {
|
|||||||
|
|
||||||
public boolean isValid(int row, int col, int n, char[][] chessboard) {
|
public boolean isValid(int row, int col, int n, char[][] chessboard) {
|
||||||
// 检查列
|
// 检查列
|
||||||
for (int i=0; i<n; ++i) {
|
for (int i=0; i<row; ++i) { // 相当于剪枝
|
||||||
if (chessboard[i][col] == 'Q') {
|
if (chessboard[i][col] == 'Q') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -505,21 +505,51 @@ var maxdepth = function(root) {
|
|||||||
|
|
||||||
二叉树最大深度层级遍历
|
二叉树最大深度层级遍历
|
||||||
```javascript
|
```javascript
|
||||||
var maxdepth = function(root) {
|
var maxDepth = function(root) {
|
||||||
//使用递归的方法 递归三部曲
|
if(!root) return 0
|
||||||
//1. 确定递归函数的参数和返回值
|
let count = 0
|
||||||
const getdepth=function(node){
|
const queue = [root]
|
||||||
//2. 确定终止条件
|
while(queue.length) {
|
||||||
if(node===null){
|
let size = queue.length
|
||||||
return 0;
|
/* 层数+1 */
|
||||||
|
count++
|
||||||
|
while(size--) {
|
||||||
|
let node = queue.shift();
|
||||||
|
node.left && queue.push(node.left);
|
||||||
|
node.right && queue.push(node.right);
|
||||||
}
|
}
|
||||||
//3. 确定单层逻辑
|
|
||||||
let leftdepth=getdepth(node.left);
|
|
||||||
let rightdepth=getdepth(node.right);
|
|
||||||
let depth=1+math.max(leftdepth,rightdepth);
|
|
||||||
return depth;
|
|
||||||
}
|
}
|
||||||
return getDepth(root);
|
return count
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
N叉树的最大深度 递归写法
|
||||||
|
```js
|
||||||
|
var maxDepth = function(root) {
|
||||||
|
if(!root) return 0
|
||||||
|
let depth = 0
|
||||||
|
for(let node of root.children) {
|
||||||
|
depth = Math.max(depth, maxDepth(node))
|
||||||
|
}
|
||||||
|
return depth + 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
N叉树的最大深度 层序遍历
|
||||||
|
```js
|
||||||
|
var maxDepth = function(root) {
|
||||||
|
if(!root) return 0
|
||||||
|
let count = 0
|
||||||
|
let queue = [root]
|
||||||
|
while(queue.length) {
|
||||||
|
let size = queue.length
|
||||||
|
count++
|
||||||
|
while(size--) {
|
||||||
|
let node = queue.shift()
|
||||||
|
node && (queue = [...queue, ...node.children])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user