mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 13:00:22 +08:00
Merge pull request #1081 from xiaofei-2020/tree15
添加(二叉树中递归带着回溯.md):增加typescript版本
This commit is contained in:
@ -171,7 +171,7 @@ if (cur->right) {
|
|||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
### Java:
|
||||||
100. 相同的树:递归代码
|
100. 相同的树:递归代码
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -252,7 +252,7 @@ Java:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
100.相同的树
|
100.相同的树
|
||||||
> 递归法
|
> 递归法
|
||||||
@ -332,7 +332,7 @@ class Solution:
|
|||||||
self.traversal(cur.right, path+"->", result) #右 回溯就隐藏在这里
|
self.traversal(cur.right, path+"->", result) #右 回溯就隐藏在这里
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
100.相同的树
|
100.相同的树
|
||||||
```go
|
```go
|
||||||
@ -436,7 +436,7 @@ func traversal(root *TreeNode,result *[]string,path *[]int){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
### JavaScript:
|
||||||
|
|
||||||
100.相同的树
|
100.相同的树
|
||||||
```javascript
|
```javascript
|
||||||
@ -515,7 +515,53 @@ var binaryTreePaths = function(root) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
|
||||||
|
### TypeScript:
|
||||||
|
|
||||||
|
> 相同的树
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
|
||||||
|
if (p === null && q === null) return true;
|
||||||
|
if (p === null || q === null) return false;
|
||||||
|
if (p.val !== q.val) return false;
|
||||||
|
let bool1: boolean, bool2: boolean;
|
||||||
|
bool1 = isSameTree(p.left, q.left);
|
||||||
|
bool2 = isSameTree(p.right, q.right);
|
||||||
|
return bool1 && bool2;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
> 二叉树的不同路径
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function binaryTreePaths(root: TreeNode | null): string[] {
|
||||||
|
function recur(node: TreeNode, nodeSeqArr: number[], resArr: string[]): void {
|
||||||
|
nodeSeqArr.push(node.val);
|
||||||
|
if (node.left === null && node.right === null) {
|
||||||
|
resArr.push(nodeSeqArr.join('->'));
|
||||||
|
}
|
||||||
|
if (node.left !== null) {
|
||||||
|
recur(node.left, nodeSeqArr, resArr);
|
||||||
|
nodeSeqArr.pop();
|
||||||
|
}
|
||||||
|
if (node.right !== null) {
|
||||||
|
recur(node.right, nodeSeqArr, resArr);
|
||||||
|
nodeSeqArr.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let nodeSeqArr: number[] = [];
|
||||||
|
let resArr: string[] = [];
|
||||||
|
if (root === null) return resArr;
|
||||||
|
recur(root, nodeSeqArr, resArr);
|
||||||
|
return resArr;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Swift:
|
||||||
> 100.相同的树
|
> 100.相同的树
|
||||||
```swift
|
```swift
|
||||||
// 递归
|
// 递归
|
||||||
@ -571,5 +617,6 @@ func _binaryTreePaths3(_ root: TreeNode, res: inout [String], paths: inout [Int]
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user