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 #318

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 17 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
e5d694d
Update
youngyangyang04 Jul 19, 2023
9b3c234
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
youngyangyang04 Jul 19, 2023
b3d1a88
Update 0503.下一个更大元素II.md
Ainevsia Jul 8, 2023
f3f549d
Update 0042.接雨水.md
Ainevsia Jul 8, 2023
7aa2a3e
Update 0084.柱状图中最大的矩形.md
Ainevsia Jul 9, 2023
0dc6bb5
添加59.螺旋矩阵II Ruby实现
niuli1991 Jul 25, 2023
7c9fcfe
更新 时间复杂度 O(n)超时 拍版格式修复
jinbudaily Jul 27, 2023
e0c5da7
更新 单调栈系列题目 排版格式修复
jinbudaily Jul 27, 2023
dc9fb7c
更新 数组额外题目 排版格式修复
jinbudaily Jul 27, 2023
56f3780
更新 哈希表额外题目 排版格式修复
jinbudaily Jul 27, 2023
bbb2a60
更新 二叉树额外题目 排版格式修复
jinbudaily Jul 27, 2023
e9b0d46
更新 贪心与动态规划 额外题目 排版格式修复
jinbudaily Jul 27, 2023
a5eb340
更新 图论 并查集 模拟 位运算 额外题目 排版格式修复
jinbudaily Jul 27, 2023
e7c1224
合并修改 0503.下一个更大元素II
jinbudaily Jul 27, 2023
9693ad4
Update
youngyangyang04 Jul 27, 2023
4037eb6
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
youngyangyang04 Jul 27, 2023
4f632c8
Merge pull request #2211 from jinbudaily/master
youngyangyang04 Jul 27, 2023
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
Prev Previous commit
Next Next commit
更新 二叉树额外题目 排版格式修复
  • Loading branch information
jinbudaily committed Jul 27, 2023
commit bbb2a60f8a68bf03b466e3b567ada9cfcd06d24f
21 changes: 12 additions & 9 deletions problems/0100.相同的树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png)


# 思路
## 思路

在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。

Expand Down Expand Up @@ -115,7 +115,7 @@ public:

当然我可以把如上代码整理如下:

## 递归
### 递归

```CPP
class Solution {
Expand All @@ -134,7 +134,7 @@ public:
};
```

## 迭代法
### 迭代法

```CPP
class Solution {
Expand Down Expand Up @@ -166,9 +166,9 @@ public:
};
```

# 其他语言版本
## 其他语言版本

Java:
### Java:

```java
// 递归法
Expand Down Expand Up @@ -205,7 +205,8 @@ class Solution {
}
}
```
Python:
### Python:

```python
# 递归法
class Solution:
Expand Down Expand Up @@ -236,7 +237,8 @@ class Solution:
que.append(rightNode.right)
return True
```
Go:
### Go:

> 递归法
```go
func isSameTree(p *TreeNode, q *TreeNode) bool {
Expand All @@ -258,7 +260,7 @@ func isSameTree(p *TreeNode, q *TreeNode) bool {
}
```

JavaScript:
### JavaScript:

> 递归法

Expand Down Expand Up @@ -296,7 +298,7 @@ var isSameTree = (p, q) => {
};
```

TypeScript:
### TypeScript:

> 递归法-先序遍历

Expand Down Expand Up @@ -341,3 +343,4 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

19 changes: 10 additions & 9 deletions problems/0116.填充每个节点的下一个右侧节点指针.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ struct Node {

![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png)

# 思路
## 思路

注意题目提示内容,:
* 你只能使用常量级额外空间。
* 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。

基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。

## 递归
### 递归

一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。

Expand Down Expand Up @@ -83,7 +83,7 @@ public:
};
```

## 迭代(层序遍历)
### 迭代(层序遍历)

本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。

Expand Down Expand Up @@ -114,9 +114,9 @@ public:
};
```

# 其他语言版本
## 其他语言版本

## Java
### Java

```java
// 递归法
Expand Down Expand Up @@ -169,7 +169,7 @@ class Solution {
}
```

## Python
### Python

```python
# 递归法
Expand Down Expand Up @@ -210,7 +210,7 @@ class Solution:
nodePre.next = None # 本层最后一个节点指向None
return root
```
## Go
### Go
```go
// 迭代法
func connect(root *Node) *Node {
Expand Down Expand Up @@ -259,7 +259,7 @@ func connect(root *Node) *Node {
}
```

## JavaScript
### JavaScript

```js
const connect = root => {
Expand Down Expand Up @@ -287,7 +287,7 @@ const connect = root => {
};
```

## TypeScript
### TypeScript

(注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’)

Expand Down Expand Up @@ -365,3 +365,4 @@ function connect(root: NodePro | null): NodePro | null {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

25 changes: 14 additions & 11 deletions problems/0129.求根到叶子节点数字之和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

[力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/)

# 思路
## 思路

本题和[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii)是类似的思路,做完这道题,可以顺便把[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii) 和 [112.路径总和](https://programmercarl.com/0112.路径总和.html#_112-路径总和) 做了。

Expand All @@ -24,7 +24,7 @@

那么先按递归三部曲来分析:

## 递归三部曲
### 递归三部曲

如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html)

Expand Down Expand Up @@ -116,7 +116,7 @@ path.pop_back(); // 回溯
```
**把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。

## 整体C++代码
整体C++代码

关键逻辑分析完了,整体C++代码如下:

Expand Down Expand Up @@ -162,16 +162,16 @@ public:
};
```

# 总结
## 总结

过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。

**我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!**


# 其他语言版本
## 其他语言版本

Java:
### Java:

```java
class Solution {
Expand Down Expand Up @@ -219,7 +219,8 @@ class Solution {
}
```

Python:
### Python:

```python
class Solution:
def sumNumbers(self, root: TreeNode) -> int:
Expand All @@ -246,7 +247,7 @@ class Solution:
backtrace(root)
return res
```
Go:
### Go:

```go
func sumNumbers(root *TreeNode) int {
Expand All @@ -271,7 +272,8 @@ func dfs(root *TreeNode, tmpSum int, sum *int) {



JavaScript:
### JavaScript:

```javascript
var sumNumbers = function(root) {
const listToInt = path => {
Expand Down Expand Up @@ -315,7 +317,7 @@ var sumNumbers = function(root) {
};
```

TypeScript:
### TypeScript:

```typescript
function sumNumbers(root: TreeNode | null): number {
Expand Down Expand Up @@ -351,7 +353,7 @@ function sumNumbers(root: TreeNode | null): number {
};
```

C:
### C:

```c
//sum记录总和
Expand Down Expand Up @@ -384,3 +386,4 @@ int sumNumbers(struct TreeNode* root){
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

18 changes: 11 additions & 7 deletions problems/1382.将二叉搜索树变平衡.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* 树节点的数目在 1 到 10^4 之间。
* 树节点的值互不相同,且在 1 到 10^5 之间。

# 思路
## 思路

这道题目,可以中序遍历把二叉树转变为有序数组,然后在根据有序数组构造平衡二叉搜索树。

Expand Down Expand Up @@ -71,9 +71,10 @@ public:
};
```

# 其他语言版本
## 其他语言版本

### Java:

Java:
```java
class Solution {
ArrayList <Integer> res = new ArrayList<Integer>();
Expand All @@ -99,7 +100,8 @@ class Solution {
}
}
```
Python:
### Python:

```python
class Solution:
def balanceBST(self, root: TreeNode) -> TreeNode:
Expand All @@ -121,7 +123,7 @@ class Solution:
traversal(root)
return getTree(res, 0, len(res) - 1)
```
Go:
### Go:

```go
/**
Expand Down Expand Up @@ -163,7 +165,8 @@ func balanceBST(root *TreeNode) *TreeNode {

```

JavaScript:
### JavaScript:

```javascript
var balanceBST = function(root) {
const res = [];
Expand All @@ -188,7 +191,7 @@ var balanceBST = function(root) {
};
```

TypeScript:
### TypeScript:

```typescript
function balanceBST(root: TreeNode | null): TreeNode | null {
Expand Down Expand Up @@ -218,3 +221,4 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

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