diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index c2ad9508..ce9a247c 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -8,21 +8,23 @@ # 二叉树层序遍历登场! -《代码随想录》算法视频公开课:[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 学会二叉树的层序遍历,可以一口气打完以下十题: -* 102.二叉树的层序遍历 -* 107.二叉树的层次遍历II -* 199.二叉树的右视图 -* 637.二叉树的层平均值 -* 429.N叉树的层序遍历 -* 515.在每个树行中找最大值 -* 116.填充每个节点的下一个右侧节点指针 -* 117.填充每个节点的下一个右侧节点指针II -* 104.二叉树的最大深度 -* 111.二叉树的最小深度 +* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) +* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) +* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) +* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) +* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) +* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) @@ -31,7 +33,7 @@ -# 102.二叉树的层序遍历 +## 102.二叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/) @@ -39,7 +41,7 @@ ![102.二叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144842988.png) -思路: +### 思路 我们之前讲过了三篇关于二叉树的深度优先遍历的文章: @@ -63,7 +65,7 @@ 代码如下:**这份代码也可以作为二叉树层序遍历的模板,打十个就靠它了**。 -C++代码: +c++代码如下: ```CPP class Solution { @@ -111,7 +113,9 @@ public: }; ``` -java: +### 其他语言版本 + +#### Java: ```Java // 102.二叉树的层序遍历 @@ -167,7 +171,7 @@ class Solution { } ``` -python3代码: +#### Python: ```python @@ -219,12 +223,9 @@ class Solution: self.helper(node.left, level + 1, levels) self.helper(node.right, level + 1, levels) - ``` - - -go: +#### Go: ```go /** @@ -320,7 +321,7 @@ func levelOrder(root *TreeNode) (res [][]int) { } ``` -javascript代码: +#### Javascript: ```javascript var levelOrder = function(root) { @@ -350,7 +351,7 @@ var levelOrder = function(root) { ``` -TypeScript: +#### TypeScript: ```typescript function levelOrder(root: TreeNode | null): number[][] { @@ -377,7 +378,7 @@ function levelOrder(root: TreeNode | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrder(_ root: TreeNode?) -> [[Int]] { @@ -403,7 +404,7 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] { } ``` -Scala: +#### Scala: ```scala // 102.二叉树的层序遍历 @@ -430,7 +431,7 @@ object Solution { } ``` -Rust: +#### Rust: ```rust use std::cell::RefCell; @@ -466,7 +467,7 @@ impl Solution { **此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!** -# 107.二叉树的层次遍历 II +## 107.二叉树的层次遍历 II [力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) @@ -474,7 +475,7 @@ impl Solution { ![107.二叉树的层次遍历II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151058308.png) -思路: +### 思路 相对于102.二叉树的层序遍历,就是最后把result数组反转一下就可以了。 @@ -506,7 +507,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python class Solution: @@ -537,7 +540,7 @@ class Solution: return result[::-1] ``` -Java: +#### Java: ```java // 107. 二叉树的层序遍历 II @@ -618,12 +621,10 @@ class Solution { return ans; } -} + ``` - - -go: +#### Go: ```GO /** @@ -662,7 +663,7 @@ func levelOrderBottom(root *TreeNode) [][]int { } ``` -javascript代码 +#### Javascript: ```javascript var levelOrderBottom = function(root) { @@ -688,7 +689,7 @@ var levelOrderBottom = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function levelOrderBottom(root: TreeNode | null): number[][] { @@ -711,7 +712,7 @@ function levelOrderBottom(root: TreeNode | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { @@ -737,8 +738,7 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { } ``` - -Scala: +#### Scala: ```scala // 107.二叉树的层次遍历II @@ -764,7 +764,10 @@ object Solution { res.reverse.toList } -Rust: + +``` + +#### Rust: ```rust use std::cell::RefCell; @@ -796,7 +799,7 @@ impl Solution { } ``` -# 199.二叉树的右视图 +## 199.二叉树的右视图 [力扣题目链接](https://leetcode.cn/problems/binary-tree-right-side-view/) @@ -804,7 +807,7 @@ impl Solution { ![199.二叉树的右视图](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151307377.png) -思路: +### 思路 层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。 @@ -832,7 +835,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python # Definition for a binary tree node. @@ -866,8 +871,7 @@ class Solution: return right_view ``` - -Java: +#### Java: ```java // 199.二叉树的右视图 @@ -911,7 +915,7 @@ public class N0199 { } ``` -go: +#### Go: ```GO /** @@ -945,8 +949,7 @@ func rightSideView(root *TreeNode) []int { } ``` - -javascript代码: +#### Javascript: ```javascript var rightSideView = function(root) { @@ -972,7 +975,7 @@ var rightSideView = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function rightSideView(root: TreeNode | null): number[] { @@ -992,7 +995,7 @@ function rightSideView(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func rightSideView(_ root: TreeNode?) -> [Int] { @@ -1017,7 +1020,7 @@ func rightSideView(_ root: TreeNode?) -> [Int] { } ``` -Scala: +#### Scala: ```scala // 199.二叉树的右视图 @@ -1043,7 +1046,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -1076,7 +1079,7 @@ impl Solution { } ``` -# 637.二叉树的层平均值 +## 637.二叉树的层平均值 [力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) @@ -1084,7 +1087,7 @@ impl Solution { ![637.二叉树的层平均值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151350500.png) -思路: +### 思路 本题就是层序遍历的时候把一层求个总和在取一个均值。 @@ -1115,7 +1118,9 @@ public: ``` -python代码: +### 其他语言版本 + +#### Python: ```python class Solution: @@ -1155,7 +1160,7 @@ class Solution: return averages ``` -java: +#### Java: ```java // 637. 二叉树的层平均值 @@ -1197,7 +1202,7 @@ public class N0637 { } ``` -go: +#### Go: ```GO /** @@ -1235,7 +1240,7 @@ func averageOfLevels(root *TreeNode) []float64 { } ``` -javascript代码: +#### Javascript: ```javascript var averageOfLevels = function(root) { @@ -1262,7 +1267,7 @@ var averageOfLevels = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function averageOfLevels(root: TreeNode | null): number[] { @@ -1286,7 +1291,7 @@ function averageOfLevels(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func averageOfLevels(_ root: TreeNode?) -> [Double] { @@ -1313,7 +1318,7 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] { } ``` -Scala: +#### Scala: ```scala // 637.二叉树的层平均值 @@ -1339,7 +1344,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -1372,7 +1377,7 @@ impl Solution { } ``` -# 429.N叉树的层序遍历 +## 429.N叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) @@ -1390,8 +1395,7 @@ impl Solution { [5,6] ] - -思路: +### 思路 这道题依旧是模板题,只不过一个节点有多个孩子了 @@ -1423,7 +1427,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python """ @@ -1475,7 +1481,7 @@ class Solution: return result ``` -java: +#### Java: ```java // 429. N 叉树的层序遍历 @@ -1535,8 +1541,7 @@ public class N0429 { } ``` - -go: +#### Go: ```GO /** @@ -1567,7 +1572,7 @@ func levelOrder(root *Node) [][]int { } ``` -JavaScript代码: +#### JavaScript: ```JavaScript var levelOrder = function(root) { @@ -1596,7 +1601,7 @@ var levelOrder = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function levelOrder(root: Node | null): number[][] { @@ -1618,7 +1623,7 @@ function levelOrder(root: Node | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrder(_ root: Node?) -> [[Int]] { @@ -1643,7 +1648,7 @@ func levelOrder(_ root: Node?) -> [[Int]] { } ``` -Scala: +#### Scala: ```scala // 429.N叉树的层序遍历 @@ -1672,7 +1677,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust pub struct Solution; @@ -1720,7 +1725,7 @@ impl Solution { } ``` -# 515.在每个树行中找最大值 +## 515.在每个树行中找最大值 [力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) @@ -1728,7 +1733,7 @@ impl Solution { ![515.在每个树行中找最大值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151532153.png) -思路: +### 思路 层序遍历,取每一层的最大值 @@ -1758,7 +1763,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python # Definition for a binary tree node. @@ -1794,7 +1801,7 @@ class Solution: return result ``` -java代码: +#### Java: ```java class Solution { @@ -1820,7 +1827,7 @@ class Solution { } ``` -go: +#### Go: ```GO /** @@ -1864,7 +1871,7 @@ func max(x, y int) int { } ``` -javascript代码: +#### Javascript: ```javascript var largestValues = function(root) { @@ -1890,7 +1897,7 @@ var largestValues = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function largestValues(root: TreeNode | null): number[] { @@ -1916,7 +1923,7 @@ function largestValues(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func largestValues(_ root: TreeNode?) -> [Int] { @@ -1943,7 +1950,7 @@ func largestValues(_ root: TreeNode?) -> [Int] { } ``` -Scala: +#### Scala: ```scala // 515.在每个树行中找最大值 @@ -1970,7 +1977,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -2002,7 +2009,7 @@ impl Solution { } ``` -# 116.填充每个节点的下一个右侧节点指针 +## 116.填充每个节点的下一个右侧节点指针 [力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) @@ -2024,7 +2031,7 @@ struct Node { ![116.填充每个节点的下一个右侧节点指针](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203152044855.jpg) -思路: +### 思路 本题依然是层序遍历,只不过在单层遍历的时候记录一下本层的头部节点,然后在遍历的时候让前一个节点指向本节点就可以了 @@ -2063,7 +2070,9 @@ public: }; ``` -java代码: +### 其他语言版本 + +#### Java: ```java class Solution { @@ -2093,7 +2102,7 @@ class Solution { } ``` -python代码: +#### Python: ```python """ @@ -2133,7 +2142,7 @@ class Solution: return root ``` -go: +#### Go: ```GO /** @@ -2173,7 +2182,7 @@ func connect(root *Node) *Node { ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2209,7 +2218,7 @@ var connect = function(root) { ``` -TypeScript: +#### TypeScript: ```typescript function connect(root: Node | null): Node | null { @@ -2234,7 +2243,7 @@ function connect(root: Node | null): Node | null { }; ``` -Swift: +#### Swift: ```swift func connect(_ root: Node?) -> Node? { @@ -2266,7 +2275,7 @@ func connect(_ root: Node?) -> Node? { } ``` -Scala: +#### Scala: ```scala // 116.填充每个节点的下一个右侧节点指针 @@ -2297,11 +2306,11 @@ object Solution { } ``` -# 117.填充每个节点的下一个右侧节点指针II +## 117.填充每个节点的下一个右侧节点指针II [力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) -思路: +### 思路 这道题目说是二叉树,但116题目说是完整二叉树,其实没有任何差别,一样的代码一样的逻辑一样的味道 @@ -2339,7 +2348,9 @@ public: }; ``` -Java 代码: +### 其他语言版本 + +#### Java: ```java // 二叉树之层次遍历 @@ -2377,7 +2388,7 @@ class Solution { } ``` -python代码: +#### Python: ```python # 层序遍历解法 @@ -2420,7 +2431,7 @@ class Solution: ``` -go: +#### Go: ```GO /** @@ -2459,7 +2470,7 @@ func connect(root *Node) *Node { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2494,7 +2505,7 @@ var connect = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function connect(root: Node | null): Node | null { @@ -2519,7 +2530,7 @@ function connect(root: Node | null): Node | null { }; ``` -Swift: +#### Swift: ```swift func connect(_ root: Node?) -> Node? { @@ -2551,7 +2562,7 @@ func connect(_ root: Node?) -> Node? { } ``` -Scala: +#### Scala: ```scala // 117.填充每个节点的下一个右侧节点指针II @@ -2582,7 +2593,7 @@ object Solution { } ``` -# 104.二叉树的最大深度 +## 104.二叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) @@ -2600,7 +2611,7 @@ object Solution { 返回它的最大深度 3 。 -思路: +### 思路 使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。 @@ -2635,7 +2646,9 @@ public: }; ``` -Java: +### 其他语言版本 + +#### Java: ```Java class Solution { @@ -2661,7 +2674,7 @@ class Solution { } ``` -Python: +#### Python: ```python 3 # Definition for a binary tree node. @@ -2691,7 +2704,7 @@ class Solution: ``` -Go: +#### Go: ```go /** @@ -2726,7 +2739,7 @@ func maxDepth(root *TreeNode) int { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2759,7 +2772,7 @@ var maxDepth = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function maxDepth(root: TreeNode | null): number { @@ -2779,7 +2792,7 @@ function maxDepth(root: TreeNode | null): number { }; ``` -Swift: +#### Swift: ```swift func maxDepth(_ root: TreeNode?) -> Int { @@ -2804,7 +2817,7 @@ func maxDepth(_ root: TreeNode?) -> Int { } ``` -Scala: +#### Scala: ```scala // 104.二叉树的最大深度 @@ -2829,7 +2842,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -2859,10 +2872,12 @@ impl Solution { } ``` -# 111.二叉树的最小深度 +## 111.二叉树的最小深度 [力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) +### 思路 + 相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。 **需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点** @@ -2895,7 +2910,9 @@ public: }; ``` -Java: +### 其他语言版本 + +#### Java: ```java class Solution { @@ -2925,9 +2942,7 @@ class Solution { } ``` - - -Python 3: +#### Python: ```python 3 # Definition for a binary tree node. @@ -2960,7 +2975,7 @@ class Solution: return depth ``` -Go: +#### Go: ```go /** @@ -2999,7 +3014,7 @@ func minDepth(root *TreeNode) int { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -3035,7 +3050,7 @@ var minDepth = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function minDepth(root: TreeNode | null): number { @@ -3056,7 +3071,7 @@ function minDepth(root: TreeNode | null): number { }; ``` -Swift: +#### Swift: ```swift func minDepth(_ root: TreeNode?) -> Int { @@ -3082,7 +3097,7 @@ func minDepth(_ root: TreeNode?) -> Int { } ``` -Scala: +#### Scala: ```scala // 111.二叉树的最小深度 @@ -3108,7 +3123,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -3141,28 +3156,27 @@ impl Solution { } ``` -# 总结 +## 总结 二叉树的层序遍历,**就是图论中的广度优先搜索在二叉树中的应用**,需要借助队列来实现(此时又发现队列的一个应用了)。 来吧,一口气打十个: -* 102.二叉树的层序遍历 -* 107.二叉树的层次遍历II -* 199.二叉树的右视图 -* 637.二叉树的层平均值 -* 429.N叉树的层序遍历 -* 515.在每个树行中找最大值 -* 116.填充每个节点的下一个右侧节点指针 -* 117.填充每个节点的下一个右侧节点指针II -* 104.二叉树的最大深度 -* 111.二叉树的最小深度 +* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) +* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) +* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) +* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) +* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) +* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) **致敬叶师傅!** - -

+