更新 二叉树额外题目 排版格式修复

This commit is contained in:
jinbudaily
2023-07-27 14:37:16 +08:00
parent 56f37806ca
commit bbb2a60f8a
4 changed files with 47 additions and 36 deletions

View File

@ -19,7 +19,7 @@
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png) ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png)
# 思路 ## 思路
在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。 在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。
@ -115,7 +115,7 @@ public:
当然我可以把如上代码整理如下: 当然我可以把如上代码整理如下:
## 递归 ### 递归
```CPP ```CPP
class Solution { class Solution {
@ -134,7 +134,7 @@ public:
}; };
``` ```
## 迭代法 ### 迭代法
```CPP ```CPP
class Solution { class Solution {
@ -166,9 +166,9 @@ public:
}; };
``` ```
# 其他语言版本 ## 其他语言版本
Java ### Java
```java ```java
// 递归法 // 递归法
@ -205,7 +205,8 @@ class Solution {
} }
} }
``` ```
Python ### Python
```python ```python
# 递归法 # 递归法
class Solution: class Solution:
@ -236,7 +237,8 @@ class Solution:
que.append(rightNode.right) que.append(rightNode.right)
return True return True
``` ```
Go ### Go
> 递归法 > 递归法
```go ```go
func isSameTree(p *TreeNode, q *TreeNode) bool { func isSameTree(p *TreeNode, q *TreeNode) bool {
@ -258,7 +260,7 @@ func isSameTree(p *TreeNode, q *TreeNode) bool {
} }
``` ```
JavaScript ### JavaScript
> 递归法 > 递归法
@ -296,7 +298,7 @@ var isSameTree = (p, q) => {
}; };
``` ```
TypeScript: ### TypeScript:
> 递归法-先序遍历 > 递归法-先序遍历
@ -341,3 +343,4 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>

View File

@ -30,7 +30,7 @@ struct Node {
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png) ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png)
# 思路 ## 思路
注意题目提示内容,: 注意题目提示内容,:
* 你只能使用常量级额外空间。 * 你只能使用常量级额外空间。
@ -38,7 +38,7 @@ struct Node {
基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。 基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。
## 递归 ### 递归
一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。 一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。
@ -83,7 +83,7 @@ public:
}; };
``` ```
## 迭代(层序遍历) ### 迭代(层序遍历)
本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。 本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。
@ -114,9 +114,9 @@ public:
}; };
``` ```
# 其他语言版本 ## 其他语言版本
## Java ### Java
```java ```java
// 递归法 // 递归法
@ -169,7 +169,7 @@ class Solution {
} }
``` ```
## Python ### Python
```python ```python
# 递归法 # 递归法
@ -210,7 +210,7 @@ class Solution:
nodePre.next = None # 本层最后一个节点指向None nodePre.next = None # 本层最后一个节点指向None
return root return root
``` ```
## Go ### Go
```go ```go
// 迭代法 // 迭代法
func connect(root *Node) *Node { func connect(root *Node) *Node {
@ -259,7 +259,7 @@ func connect(root *Node) *Node {
} }
``` ```
## JavaScript ### JavaScript
```js ```js
const connect = root => { const connect = root => {
@ -287,7 +287,7 @@ const connect = root => {
}; };
``` ```
## TypeScript ### TypeScript
命名空间Node与typescript中内置类型冲突这里改成了NodePro 命名空间Node与typescript中内置类型冲突这里改成了NodePro
@ -365,3 +365,4 @@ function connect(root: NodePro | null): NodePro | null {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>

View File

@ -10,7 +10,7 @@
[力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/) [力扣题目链接](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-路径总和) 做了。 本题和[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-路径总和) 做了。
@ -24,7 +24,7 @@
那么先按递归三部曲来分析: 那么先按递归三部曲来分析:
## 递归三部曲 ### 递归三部曲
如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html) 如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html)
@ -116,7 +116,7 @@ path.pop_back(); // 回溯
``` ```
**把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。 **把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。
## 整体C++代码 整体C++代码
关键逻辑分析完了整体C++代码如下: 关键逻辑分析完了整体C++代码如下:
@ -162,16 +162,16 @@ public:
}; };
``` ```
# 总结 ## 总结
过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。 过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。
**我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!** **我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!**
# 其他语言版本 ## 其他语言版本
Java ### Java
```java ```java
class Solution { class Solution {
@ -219,7 +219,8 @@ class Solution {
} }
``` ```
Python ### Python
```python ```python
class Solution: class Solution:
def sumNumbers(self, root: TreeNode) -> int: def sumNumbers(self, root: TreeNode) -> int:
@ -246,7 +247,7 @@ class Solution:
backtrace(root) backtrace(root)
return res return res
``` ```
Go ### Go
```go ```go
func sumNumbers(root *TreeNode) int { func sumNumbers(root *TreeNode) int {
@ -271,7 +272,8 @@ func dfs(root *TreeNode, tmpSum int, sum *int) {
JavaScript ### JavaScript
```javascript ```javascript
var sumNumbers = function(root) { var sumNumbers = function(root) {
const listToInt = path => { const listToInt = path => {
@ -315,7 +317,7 @@ var sumNumbers = function(root) {
}; };
``` ```
TypeScript ### TypeScript
```typescript ```typescript
function sumNumbers(root: TreeNode | null): number { function sumNumbers(root: TreeNode | null): number {
@ -351,7 +353,7 @@ function sumNumbers(root: TreeNode | null): number {
}; };
``` ```
C: ### C:
```c ```c
//sum记录总和 //sum记录总和
@ -384,3 +386,4 @@ int sumNumbers(struct TreeNode* root){
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>

View File

@ -28,7 +28,7 @@
* 树节点的数目在 1  10^4 之间。 * 树节点的数目在 1  10^4 之间。
* 树节点的值互不相同且在 1  10^5 之间。 * 树节点的值互不相同且在 1  10^5 之间。
# 思路 ## 思路
这道题目,可以中序遍历把二叉树转变为有序数组,然后在根据有序数组构造平衡二叉搜索树。 这道题目,可以中序遍历把二叉树转变为有序数组,然后在根据有序数组构造平衡二叉搜索树。
@ -71,9 +71,10 @@ public:
}; };
``` ```
# 其他语言版本 ## 其他语言版本
### Java
Java
```java ```java
class Solution { class Solution {
ArrayList <Integer> res = new ArrayList<Integer>(); ArrayList <Integer> res = new ArrayList<Integer>();
@ -99,7 +100,8 @@ class Solution {
} }
} }
``` ```
Python ### Python
```python ```python
class Solution: class Solution:
def balanceBST(self, root: TreeNode) -> TreeNode: def balanceBST(self, root: TreeNode) -> TreeNode:
@ -121,7 +123,7 @@ class Solution:
traversal(root) traversal(root)
return getTree(res, 0, len(res) - 1) return getTree(res, 0, len(res) - 1)
``` ```
Go ### Go
```go ```go
/** /**
@ -163,7 +165,8 @@ func balanceBST(root *TreeNode) *TreeNode {
``` ```
JavaScript ### JavaScript
```javascript ```javascript
var balanceBST = function(root) { var balanceBST = function(root) {
const res = []; const res = [];
@ -188,7 +191,7 @@ var balanceBST = function(root) {
}; };
``` ```
TypeScript ### TypeScript
```typescript ```typescript
function balanceBST(root: TreeNode | null): TreeNode | null { function balanceBST(root: TreeNode | null): TreeNode | null {
@ -218,3 +221,4 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>