更新图床

This commit is contained in:
programmercarl
2023-03-10 14:02:32 +08:00
parent 2a9b627a90
commit 17cb4b45c7
134 changed files with 1169 additions and 829 deletions

View File

@@ -1,9 +1,11 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
# 二叉树层序遍历登场!
《代码随想录》算法视频公开课:[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频在看本篇题解,更有助于大家对本题的理解。
@@ -35,7 +37,7 @@
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
![102.二叉树的层序遍历](https://img-blog.csdnimg.cn/20210203144842988.png)
![102.二叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144842988.png)
思路:
@@ -87,6 +89,7 @@ public:
}
};
```
```CPP
# 递归法
class Solution {
@@ -108,7 +111,7 @@ public:
};
```
java:
java:
```Java
// 102.二叉树的层序遍历
@@ -168,7 +171,6 @@ python3代码
```python
class Solution:
"""二叉树层序遍历迭代解法"""
@@ -176,10 +178,10 @@ class Solution:
results = []
if not root:
return results
from collections import deque
que = deque([root])
while que:
size = len(que)
result = []
@@ -194,6 +196,7 @@ class Solution:
return results
```
```python
# 递归法
class Solution:
@@ -209,7 +212,7 @@ class Solution:
return res
```
go:
go:
```go
/**
@@ -252,9 +255,9 @@ func levelOrder(root *TreeNode) [][]int {
}
queue := list.New()
queue.PushBack(root)
var tmpArr []int
for queue.Len() > 0 {
length := queue.Len() //保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数)
for i := 0; i < length; i++ {
@@ -270,7 +273,7 @@ func levelOrder(root *TreeNode) [][]int {
res = append(res, tmpArr) //放入结果集
tmpArr = []int{} //清空层的数据
}
return res
}
@@ -279,7 +282,7 @@ func levelOrder(root *TreeNode) [][]int {
*/
func levelOrder(root *TreeNode) (res [][]int) {
if root == nil {
return
return
}
curLevel := []*TreeNode{root} // 存放当前层节点
@@ -318,7 +321,7 @@ var levelOrder = function(root) {
while(queue.length !== 0) {
// 记录当前层级节点数
let length = queue.length;
//存放每一层的节点
//存放每一层的节点
let curLevel = [];
for(let i = 0;i < length; i++) {
let node = queue.shift();
@@ -387,7 +390,9 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
return result
}
```
Scala:
```scala
// 102.二叉树的层序遍历
object Solution {
@@ -455,7 +460,7 @@ impl Solution {
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
![107.二叉树的层次遍历II](https://img-blog.csdnimg.cn/20210203151058308.png)
![107.二叉树的层次遍历II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151058308.png)
思路:
@@ -488,6 +493,7 @@ public:
}
};
```
python代码
```python
@@ -498,10 +504,10 @@ class Solution:
results = []
if not root:
return results
from collections import deque
que = deque([root])
while que:
result = []
for _ in range(len(que)):
@@ -572,7 +578,7 @@ class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
// 利用链表可以进行 O(1) 头部插入, 这样最后答案不需要再反转
LinkedList<List<Integer>> ans = new LinkedList<>();
Queue<TreeNode> q = new LinkedList<>();
if (root != null) q.offer(root);
@@ -584,9 +590,9 @@ class Solution {
for (int i = 0; i < size; i ++) {
TreeNode node = q.poll();
temp.add(node.val);
if (node.left != null) q.offer(node.left);
if (node.right != null) q.offer(node.right);
@@ -616,7 +622,7 @@ func levelOrderBottom(root *TreeNode) [][]int {
return res
}
queue.PushBack(root)
for queue.Len() > 0 {
length := queue.Len()
tmp := []int{}
@@ -632,12 +638,12 @@ func levelOrderBottom(root *TreeNode) [][]int {
}
res=append(res, tmp)
}
//反转结果集
for i:=0; i<len(res)/2; i++ {
res[i], res[len(res)-i-1] = res[len(res)-i-1], res[i]
}
return res
}
```
@@ -719,6 +725,7 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
Scala:
```scala
// 107.二叉树的层次遍历II
object Solution {
@@ -781,7 +788,7 @@ impl Solution {
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
![199.二叉树的右视图](https://img-blog.csdnimg.cn/20210203151307377.png)
![199.二叉树的右视图](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151307377.png)
思路:
@@ -810,6 +817,7 @@ public:
}
};
```
python代码
```python
@@ -817,7 +825,7 @@ class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:
return []
# deque来自collections模块不在力扣平台时需要手动写入
# 'from collections import deque' 导入
# deque相比list的好处是list的pop(0)是O(n)复杂度deque的popleft()是O(1)复杂度
@@ -837,15 +845,15 @@ class Solution:
quene.append(node.left)
if node.right:
quene.append(node.right)
return out_list
# 执行用时36 ms, 在所有 Python3 提交中击败了89.47%的用户
# 内存消耗14.6 MB, 在所有 Python3 提交中击败了96.65%的用户
```
Java:
Java:
```java
// 199.二叉树的右视图
@@ -889,10 +897,9 @@ public class N0199 {
}
```
go:
go:
```GO
/**
199. 二叉树的右视图
*/
@@ -932,7 +939,7 @@ var rightSideView = function(root) {
//二叉树右视图 只需要把每一层最后一个节点存储到res数组
let res = [], queue = [];
queue.push(root);
while(queue.length && root!==null) {
// 记录当前层级节点个数
let length = queue.length;
@@ -946,7 +953,7 @@ var rightSideView = function(root) {
node.right && queue.push(node.right);
}
}
return res;
};
```
@@ -997,6 +1004,7 @@ func rightSideView(_ root: TreeNode?) -> [Int] {
```
Scala:
```scala
// 199.二叉树的右视图
object Solution {
@@ -1060,7 +1068,7 @@ impl Solution {
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。
![637.二叉树的层平均值](https://img-blog.csdnimg.cn/20210203151350500.png)
![637.二叉树的层平均值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151350500.png)
思路:
@@ -1103,10 +1111,10 @@ class Solution:
results = []
if not root:
return results
from collections import deque
que = deque([root])
while que:
size = len(que)
sum_ = 0
@@ -1124,8 +1132,7 @@ class Solution:
java:
```java
```java
// 637. 二叉树的层平均值
public class N0637 {
@@ -1210,7 +1217,7 @@ var averageOfLevels = function(root) {
//层级平均值
let res = [], queue = [];
queue.push(root);
while(queue.length && root!==null) {
//每一层节点个数
let length = queue.length;
@@ -1225,7 +1232,7 @@ var averageOfLevels = function(root) {
//每一层的平均值存入数组res
res.push(sum/length);
}
return res;
};
```
@@ -1280,7 +1287,9 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] {
return result
}
```
Scala:
```scala
// 637.二叉树的层平均值
object Solution {
@@ -1346,7 +1355,7 @@ impl Solution {
例如,给定一个 3叉树 :
![429. N叉树的层序遍历](https://img-blog.csdnimg.cn/20210203151439168.png)
![429. N叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151439168.png)
返回其层序遍历:
@@ -1399,10 +1408,10 @@ class Solution:
results = []
if not root:
return results
from collections import deque
que = deque([root])
while que:
result = []
for _ in range(len(que)):
@@ -1426,16 +1435,16 @@ class Solution:
def traversal(root,depth):
if len(result)==depth:result.append([])
result[depth].append(root.val)
if root.children:
if root.children:
for i in range(len(root.children)):traversal(root.children[i],depth+1)
traversal(root,0)
return result
return result
```
java:
```java
```java
// 429. N 叉树的层序遍历
public class N0429 {
/**
@@ -1520,7 +1529,7 @@ func levelOrder(root *Node) [][]int {
}
res = append(res, tmp)
}
return res
}
```
@@ -1532,7 +1541,7 @@ var levelOrder = function(root) {
//每一层可能有2个以上,所以不再使用node.left node.right
let res = [], queue = [];
queue.push(root);
while(queue.length && root!==null) {
//记录每一层节点个数还是和二叉树一致
let length = queue.length;
@@ -1541,7 +1550,7 @@ var levelOrder = function(root) {
while(length--) {
let node = queue.shift();
curLevel.push(node.val);
//这里不再是 ndoe.left node.right 而是循坏node.children
for(let item of node.children){
item && queue.push(item);
@@ -1549,7 +1558,7 @@ var levelOrder = function(root) {
}
res.push(curLevel);
}
return res;
};
```
@@ -1602,6 +1611,7 @@ func levelOrder(_ root: Node?) -> [[Int]] {
```
Scala:
```scala
// 429.N叉树的层序遍历
object Solution {
@@ -1683,7 +1693,7 @@ impl Solution {
您需要在二叉树的每一行中找到最大的值。
![515.在每个树行中找最大值](https://img-blog.csdnimg.cn/20210203151532153.png)
![515.在每个树行中找最大值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151532153.png)
思路:
@@ -1714,6 +1724,7 @@ public:
}
};
```
python代码
```python
@@ -1734,6 +1745,7 @@ class Solution:
out_list.append(max(in_list))
return out_list
```
java代码
```java
@@ -1754,7 +1766,7 @@ class Solution {
if(node.right != null) queue.offer(node.right);
}
result.add(max);
}
}
return result;
}
}
@@ -1811,8 +1823,8 @@ var largestValues = function(root) {
//使用层序遍历
let res = [], queue = [];
queue.push(root);
while(root !== null && queue.length) {
while(root !== null && queue.length) {
//设置max初始值就是队列的第一个元素
let max = queue[0].val;
let length = queue.length;
@@ -1825,7 +1837,7 @@ var largestValues = function(root) {
//把每一层的最大值放到res数组
res.push(max);
}
return res;
};
```
@@ -1884,6 +1896,7 @@ func largestValues(_ root: TreeNode?) -> [Int] {
```
Scala:
```scala
// 515.在每个树行中找最大值
object Solution {
@@ -1959,9 +1972,9 @@ struct Node {
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
初始状态下,所有 next 指针都被设置为 NULL。
初始状态下,所有 next 指针都被设置为 NULL。
![116.填充每个节点的下一个右侧节点指针](https://img-blog.csdnimg.cn/20210203152044855.jpg)
![116.填充每个节点的下一个右侧节点指针](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203152044855.jpg)
思路:
@@ -2009,24 +2022,24 @@ class Solution {
public Node connect(Node root) {
Queue<Node> tmpQueue = new LinkedList<Node>();
if (root != null) tmpQueue.add(root);
while (tmpQueue.size() != 0){
int size = tmpQueue.size();
Node cur = tmpQueue.poll();
if (cur.left != null) tmpQueue.add(cur.left);
if (cur.right != null) tmpQueue.add(cur.right);
for (int index = 1; index < size; index++){
Node next = tmpQueue.poll();
if (next.left != null) tmpQueue.add(next.left);
if (next.right != null) tmpQueue.add(next.right);
cur.next = next;
cur = next;
}
}
return root;
}
}
@@ -2067,6 +2080,7 @@ class Solution:
first = first.left # 从本层扩展到下一层
return root
```
go:
```GO
@@ -2108,8 +2122,8 @@ func connect(root *Node) *Node {
```
JavaScript:
```javascript
```javascript
/**
* // Definition for a Node.
* function Node(val, left, right, next) {
@@ -2142,6 +2156,7 @@ var connect = function(root) {
};
```
TypeScript:
```typescript
@@ -2200,6 +2215,7 @@ func connect(_ root: Node?) -> Node? {
```
Scala:
```scala
// 116.填充每个节点的下一个右侧节点指针
object Solution {
@@ -2228,6 +2244,7 @@ object Solution {
}
}
```
# 117.填充每个节点的下一个右侧节点指针II
[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/)
@@ -2284,7 +2301,7 @@ class Solution {
int size = queue.size();
Node node = null;
Node nodePre = null;
for (int i = 0; i < size; i++) {
if (i == 0) {
nodePre = queue.poll(); // 取出本层头一个节点
@@ -2307,6 +2324,7 @@ class Solution {
}
}
```
python代码
```python
@@ -2329,6 +2347,7 @@ class Solution:
return root
```
go:
```GO
@@ -2369,6 +2388,7 @@ func connect(root *Node) *Node {
```
JavaScript:
```javascript
/**
* // Definition for a Node.
@@ -2401,6 +2421,7 @@ var connect = function(root) {
return root;
};
```
TypeScript:
```typescript
@@ -2459,6 +2480,7 @@ func connect(_ root: Node?) -> Node? {
```
Scala:
```scala
// 117.填充每个节点的下一个右侧节点指针II
object Solution {
@@ -2487,6 +2509,7 @@ object Solution {
}
}
```
# 104.二叉树的最大深度
[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
@@ -2501,7 +2524,7 @@ object Solution {
给定二叉树 [3,9,20,null,null,15,7]
![104. 二叉树的最大深度](https://img-blog.csdnimg.cn/20210203153031914.png)
![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310134849764.png)
返回它的最大深度 3 。
@@ -2511,7 +2534,7 @@ object Solution {
在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示:
![层序遍历](https://img-blog.csdnimg.cn/20200810193056585.png)
![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585-20230310134854803.png)
所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。
@@ -2540,7 +2563,8 @@ public:
};
```
Java
Java
```Java
class Solution {
public int maxDepth(TreeNode root) {
@@ -2566,12 +2590,13 @@ class Solution {
```
Python
```python 3
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if root == None:
return 0
queue_ = [root]
depth = 0
while queue_:
@@ -2583,7 +2608,7 @@ class Solution:
if cur.left: queue_.append(cur.left)
if cur.right: queue_.append(cur.right)
depth += 1
return depth
```
@@ -2623,6 +2648,7 @@ func maxDepth(root *TreeNode) int {
```
JavaScript
```javascript
/**
* Definition for a binary tree node.
@@ -2700,6 +2726,7 @@ func maxDepth(_ root: TreeNode?) -> Int {
```
Scala:
```scala
// 104.二叉树的最大深度
object Solution {
@@ -2789,7 +2816,8 @@ public:
};
```
Java
Java
```java
class Solution {
public int minDepth(TreeNode root){
@@ -2838,7 +2866,7 @@ class Solution:
queue_ = [(root,1)]
while queue_:
cur, depth = queue_.pop(0)
if cur.left == None and cur.right == None:
return depth
#先左子节点,由于左子节点没有孩子,则就是这一层了
@@ -2884,12 +2912,13 @@ func minDepth(root *TreeNode) int {
}
ans++//记录层数
}
return ans+1
}
```
JavaScript
```javascript
/**
* Definition for a binary tree node.
@@ -2972,6 +3001,7 @@ func minDepth(_ root: TreeNode?) -> Int {
```
Scala:
```scala
// 111.二叉树的最小深度
object Solution {