update 0226.翻转二叉树: 优化代码风格

This commit is contained in:
Yuhao Ju
2022-11-29 14:41:26 +08:00
committed by GitHub
parent 26aec30440
commit 8ca827b6f9

View File

@ -104,7 +104,7 @@ public:
### 深度优先遍历 ### 深度优先遍历
[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)中给出了前中后序迭代方式的写法,所以本可以很轻松的出如下迭代法的代码: [二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)中给出了前中后序迭代方式的写法,所以本可以很轻松的出如下迭代法的代码:
C++代码迭代法(前序遍历) C++代码迭代法(前序遍历)
@ -126,7 +126,7 @@ public:
} }
}; };
``` ```
如果这个代码看不懂的话可以回顾一下[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)。 如果这个代码看不懂的话可以回顾一下[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)。
我们在[二叉树:前中后序迭代方式的统一写法](https://programmercarl.com/二叉树的统一迭代法.html)中介绍了统一的写法,所以,本题也只需将文中的代码少做修改便可。 我们在[二叉树:前中后序迭代方式的统一写法](https://programmercarl.com/二叉树的统一迭代法.html)中介绍了统一的写法,所以,本题也只需将文中的代码少做修改便可。
@ -257,8 +257,7 @@ public:
## 其他语言版本 ## 其他语言版本
### Java Java
```Java ```Java
//DFS递归 //DFS递归
class Solution { class Solution {
@ -294,8 +293,8 @@ class Solution {
while (size-- > 0) { while (size-- > 0) {
TreeNode node = deque.poll(); TreeNode node = deque.poll();
swap(node); swap(node);
if (node.left != null) {deque.offer(node.left);} if (node.left != null) deque.offer(node.left);
if (node.right != null) {deque.offer(node.right);} if (node.right != null) deque.offer(node.right);
} }
} }
return root; return root;
@ -309,7 +308,7 @@ class Solution {
} }
``` ```
### Python Python
递归法:前序遍历: 递归法:前序遍历:
```python ```python
@ -361,10 +360,9 @@ class Solution:
return root return root
``` ```
### Go Go
递归版本的前序遍历 递归版本的前序遍历
```Go ```Go
func invertTree(root *TreeNode) *TreeNode { func invertTree(root *TreeNode) *TreeNode {
if root == nil { if root == nil {
@ -386,9 +384,11 @@ func invertTree(root *TreeNode) *TreeNode {
if root == nil { if root == nil {
return root return root
} }
invertTree(root.Left) //遍历左节点 invertTree(root.Left) //遍历左节点
invertTree(root.Right) //遍历右节点 invertTree(root.Right) //遍历右节点
root.Left, root.Right = root.Right, root.Left //交换 root.Left, root.Right = root.Right, root.Left //交换
return root return root
} }
``` ```
@ -409,6 +409,7 @@ func invertTree(root *TreeNode) *TreeNode {
stack = stack[:len(stack)-1] stack = stack[:len(stack)-1]
node = node.Right node = node.Right
} }
return root return root
} }
``` ```
@ -436,6 +437,7 @@ func invertTree(root *TreeNode) *TreeNode {
node = node.Right node = node.Right
} }
} }
return root return root
} }
``` ```
@ -467,7 +469,7 @@ func invertTree(root *TreeNode) *TreeNode {
} }
``` ```
### JavaScript JavaScript
使用递归版本的前序遍历 使用递归版本的前序遍历
```javascript ```javascript
@ -548,7 +550,7 @@ var invertTree = function(root) {
}; };
``` ```
### TypeScript TypeScript
递归法: 递归法:
@ -675,7 +677,7 @@ function invertTree(root: TreeNode | null): TreeNode | null {
}; };
``` ```
### C C
递归法 递归法
```c ```c
@ -722,7 +724,7 @@ struct TreeNode* invertTree(struct TreeNode* root){
} }
``` ```
### Swift Swift
```swift ```swift
// 前序遍历-递归 // 前序遍历-递归
func invertTree(_ root: TreeNode?) -> TreeNode? { func invertTree(_ root: TreeNode?) -> TreeNode? {
@ -760,7 +762,7 @@ func invertTree1(_ root: TreeNode?) -> TreeNode? {
} }
``` ```
### Swift Swift
深度优先递归。 深度优先递归。
@ -808,9 +810,6 @@ func invertTree(_ root: TreeNode?) -> TreeNode? {
return root return root
} }
``` ```
### Scala
深度优先遍历(前序遍历): 深度优先遍历(前序遍历):
```scala ```scala
object Solution { object Solution {
@ -857,7 +856,7 @@ object Solution {
} }
``` ```
### rust rust
```rust ```rust
impl Solution { impl Solution {