Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 8 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 93 additions & 6 deletions problems/0104.二叉树的最大深度.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ class solution:


## go

### 104.二叉树的最大深度
```go
/**
* definition for a binary tree node.
Expand Down Expand Up @@ -548,6 +548,8 @@ func maxdepth(root *treenode) int {

## javascript

### 104.二叉树的最大深度

```javascript
var maxdepth = function(root) {
if (root === null) return 0;
Expand Down Expand Up @@ -595,6 +597,8 @@ var maxDepth = function(root) {
};
```

### 559.n叉树的最大深度

N叉树的最大深度 递归写法
```js
var maxDepth = function(root) {
Expand Down Expand Up @@ -627,9 +631,9 @@ var maxDepth = function(root) {
};
```

## TypeScript:
## TypeScript

> 二叉树的最大深度:
### 104.二叉树的最大深度

```typescript
// 后续遍历(自下而上)
Expand Down Expand Up @@ -672,7 +676,7 @@ function maxDepth(root: TreeNode | null): number {
};
```

> N叉树的最大深度
### 559.n叉树的最大深度

```typescript
// 后续遍历(自下而上)
Expand Down Expand Up @@ -702,6 +706,8 @@ function maxDepth(root: TreeNode | null): number {

## C

### 104.二叉树的最大深度

二叉树最大深度递归
```c
int maxDepth(struct TreeNode* root){
Expand Down Expand Up @@ -758,7 +764,8 @@ int maxDepth(struct TreeNode* root){

## Swift

>二叉树最大深度
### 104.二叉树的最大深度

```swift
// 递归 - 后序
func maxDepth1(_ root: TreeNode?) -> Int {
Expand Down Expand Up @@ -797,7 +804,8 @@ func maxDepth(_ root: TreeNode?) -> Int {
}
```

>N叉树最大深度
### 559.n叉树的最大深度

```swift
// 递归
func maxDepth(_ root: Node?) -> Int {
Expand Down Expand Up @@ -833,5 +841,84 @@ func maxDepth1(_ root: Node?) -> Int {
}
```

## Scala

### 104.二叉树的最大深度
递归法:
```scala
object Solution {
def maxDepth(root: TreeNode): Int = {
def process(curNode: TreeNode): Int = {
if (curNode == null) return 0
// 递归左节点和右节点,返回最大的,最后+1
math.max(process(curNode.left), process(curNode.right)) + 1
}
// 调用递归方法,return关键字可以省略
process(root)
}
}
```

迭代法:
```scala
object Solution {
import scala.collection.mutable
def maxDepth(root: TreeNode): Int = {
var depth = 0
if (root == null) return depth
val queue = mutable.Queue[TreeNode]()
queue.enqueue(root)
while (!queue.isEmpty) {
val len = queue.size
for (i <- 0 until len) {
val curNode = queue.dequeue()
if (curNode.left != null) queue.enqueue(curNode.left)
if (curNode.right != null) queue.enqueue(curNode.right)
}
depth += 1 // 只要有层次就+=1
}
depth
}
}
```

### 559.n叉树的最大深度

递归法:
```scala
object Solution {
def maxDepth(root: Node): Int = {
if (root == null) return 0
var depth = 0
for (node <- root.children) {
depth = math.max(depth, maxDepth(node))
}
depth + 1
}
}
```

迭代法: (层序遍历)
```scala
object Solution {
import scala.collection.mutable
def maxDepth(root: Node): Int = {
if (root == null) return 0
var depth = 0
val queue = mutable.Queue[Node]()
queue.enqueue(root)
while (!queue.isEmpty) {
val len = queue.size
depth += 1
for (i <- 0 until len) {
val curNode = queue.dequeue()
for (node <- curNode.children) queue.enqueue(node)
}
}
depth
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
41 changes: 41 additions & 0 deletions problems/0111.二叉树的最小深度.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,46 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```


## Scala

递归法:
```scala
object Solution {
def minDepth(root: TreeNode): Int = {
if (root == null) return 0
if (root.left == null && root.right != null) return 1 + minDepth(root.right)
if (root.left != null && root.right == null) return 1 + minDepth(root.left)
// 如果两侧都不为空,则取最小值,return关键字可以省略
1 + math.min(minDepth(root.left), minDepth(root.right))
}
}
```

迭代法:
```scala
object Solution {
import scala.collection.mutable
def minDepth(root: TreeNode): Int = {
if (root == null) return 0
var depth = 0
val queue = mutable.Queue[TreeNode]()
queue.enqueue(root)
while (!queue.isEmpty) {
depth += 1
val len = queue.size
for (i <- 0 until len) {
val curNode = queue.dequeue()
if (curNode.left != null) queue.enqueue(curNode.left)
if (curNode.right != null) queue.enqueue(curNode.right)
if (curNode.left == null && curNode.right == null) return depth
}
}
depth
}
}
```

rust:
```rust
impl Solution {
Expand Down Expand Up @@ -550,6 +590,7 @@ impl Solution {
}
min_depth
}

```

-----------------------
Expand Down
31 changes: 31 additions & 0 deletions problems/0496.下一个更大元素I.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,36 @@ var nextGreaterElement = function (nums1, nums2) {
};
```

TypeScript:

```typescript
function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
const resArr: number[] = new Array(nums1.length).fill(-1);
const stack: number[] = [];
const helperMap: Map<number, number> = new Map();
nums1.forEach((num, index) => {
helperMap.set(num, index);
})
stack.push(0);
for (let i = 1, length = nums2.length; i < length; i++) {
let top = stack[stack.length - 1];
while (stack.length > 0 && nums2[top] < nums2[i]) {
let index = helperMap.get(nums2[top]);
if (index !== undefined) {
resArr[index] = nums2[i];
}
stack.pop();
top = stack[stack.length - 1];
}
if (helperMap.get(nums2[i]) !== undefined) {
stack.push(i);
}
}
return resArr;
};
```



-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
26 changes: 26 additions & 0 deletions problems/0503.下一个更大元素II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,31 @@ var nextGreaterElements = function (nums) {
return res;
};
```
TypeScript:

```typescript
function nextGreaterElements(nums: number[]): number[] {
const length: number = nums.length;
const stack: number[] = [];
stack.push(0);
const resArr: number[] = new Array(length).fill(-1);
for (let i = 1; i < length * 2; i++) {
const index = i % length;
let top = stack[stack.length - 1];
while (stack.length > 0 && nums[top] < nums[index]) {
resArr[top] = nums[index];
stack.pop();
top = stack[stack.length - 1];
}
if (i < length) {
stack.push(i);
}
}
return resArr;
};
```



-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

AltStyle によって変換されたページ (->オリジナル) /