Skip to content

Commit 1a99325

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 6586f9a + 48128fd commit 1a99325

File tree

3 files changed

+181
-2
lines changed

3 files changed

+181
-2
lines changed

problems/0106.从中序与后序遍历序列构造二叉树.md

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ class Solution:
684684
root.right = self.buildTree(preorder_right, inorder_right)
685685

686686
return root
687-
```
687+
```
688688

689689
106.从中序与后序遍历序列构造二叉树
690690

@@ -716,7 +716,7 @@ class Solution:
716716
root.right = self.buildTree(inorder_right, postorder_right)
717717

718718
return root
719-
```
719+
```
720720

721721
## Go
722722

@@ -816,6 +816,7 @@ var buildTree = function(preorder, inorder) {
816816

817817
## C
818818
106 从中序与后序遍历序列构造二叉树
819+
819820
```c
820821
int linearSearch(int* arr, int arrSize, int key) {
821822
int i;
@@ -847,6 +848,7 @@ struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int po
847848
```
848849
849850
105 从前序与中序遍历序列构造二叉树
851+
850852
```c
851853
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){
852854
// 递归结束条件:传入的数组大小为0
@@ -889,5 +891,100 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in
889891
}
890892
```
891893

894+
## Swift
895+
896+
105 从前序与中序遍历序列构造二叉树
897+
898+
```swift
899+
class Solution {
900+
func buildTree(_ preorder: [Int], _ inorder: [Int]) -> TreeNode? {
901+
return helper(preorder: preorder,
902+
preorderBegin: 0,
903+
preorderEnd: preorder.count,
904+
inorder: inorder,
905+
inorderBegin: 0,
906+
inorderEnd: inorder.count)
907+
}
908+
909+
func helper(preorder: [Int], preorderBegin: Int, preorderEnd: Int, inorder: [Int], inorderBegin: Int, inorderEnd: Int) -> TreeNode? {
910+
if preorderBegin == preorderEnd {
911+
return nil
912+
}
913+
914+
// 前序遍历数组的第一个元素作为分割点
915+
let rootValue = preorder[preorderBegin]
916+
let root = TreeNode(rootValue)
917+
918+
919+
if preorderEnd - preorderBegin == 1 {
920+
return root
921+
}
922+
923+
var index = 0 // 从中序遍历数组中找到根节点的下标
924+
if let ind = inorder.firstIndex(of: rootValue) {
925+
index = ind
926+
}
927+
928+
// 递归
929+
root.left = helper(preorder: preorder,
930+
preorderBegin: preorderBegin + 1,
931+
preorderEnd: preorderBegin + 1 + index - inorderBegin,
932+
inorder: inorder,
933+
inorderBegin: inorderBegin,
934+
inorderEnd: index)
935+
root.right = helper(preorder: preorder,
936+
preorderBegin: preorderBegin + 1 + index - inorderBegin,
937+
preorderEnd: preorderEnd,
938+
inorder: inorder,
939+
inorderBegin: index + 1,
940+
inorderEnd: inorderEnd)
941+
return root
942+
}
943+
}
944+
```
945+
946+
106 从中序与后序遍历序列构造二叉树
947+
948+
```swift
949+
class Solution_0106 {
950+
func buildTree(inorder: [Int], inorderBegin: Int, inorderEnd: Int, postorder: [Int], postorderBegin: Int, postorderEnd: Int) -> TreeNode? {
951+
if postorderEnd - postorderBegin < 1 {
952+
return nil
953+
}
954+
955+
// 后序遍历数组的最后一个元素作为分割点
956+
let rootValue = postorder[postorderEnd - 1]
957+
let root = TreeNode(rootValue)
958+
959+
if postorderEnd - postorderBegin == 1 {
960+
return root
961+
}
962+
963+
// 从中序遍历数组中找到根节点的下标
964+
var delimiterIndex = 0
965+
if let index = inorder.firstIndex(of: rootValue) {
966+
delimiterIndex = index
967+
}
968+
969+
root.left = buildTree(inorder: inorder,
970+
inorderBegin: inorderBegin,
971+
inorderEnd: delimiterIndex,
972+
postorder: postorder,
973+
postorderBegin: postorderBegin,
974+
postorderEnd: postorderBegin + (delimiterIndex - inorderBegin))
975+
976+
root.right = buildTree(inorder: inorder,
977+
inorderBegin: delimiterIndex + 1,
978+
inorderEnd: inorderEnd,
979+
postorder: postorder,
980+
postorderBegin: postorderBegin + (delimiterIndex - inorderBegin),
981+
postorderEnd: postorderEnd - 1)
982+
return root
983+
}
984+
}
985+
```
986+
987+
988+
892989
-----------------------
893990
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0203.移除链表元素.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,63 @@ var removeElements = function(head, val) {
302302
};
303303
```
304304

305+
TypeScript:
306+
307+
版本一(在原链表上直接删除):
308+
309+
```typescript
310+
/**
311+
* Definition for singly-linked list.
312+
* class ListNode {
313+
* val: number
314+
* next: ListNode | null
315+
* constructor(val?: number, next?: ListNode | null) {
316+
* this.val = (val===undefined ? 0 : val)
317+
* this.next = (next===undefined ? null : next)
318+
* }
319+
* }
320+
*/
321+
function removeElements(head: ListNode | null, val: number): ListNode | null {
322+
// 删除头部节点
323+
while (head !== null && head.val === val) {
324+
head = head.next;
325+
}
326+
if (head === null) return head;
327+
let pre: ListNode = head, cur: ListNode = head.next;
328+
// 删除非头部节点
329+
while (cur) {
330+
if (cur.val === val) {
331+
pre.next = cur.next;
332+
} else {
333+
pre = pre.next;
334+
}
335+
cur = cur.next;
336+
}
337+
return head;
338+
};
339+
```
340+
341+
版本二(虚拟头节点):
342+
343+
```typescript
344+
function removeElements(head: ListNode | null, val: number): ListNode | null {
345+
head = new ListNode(0, head);
346+
let pre: ListNode = head, cur: ListNode = head.next;
347+
// 删除非头部节点
348+
while (cur) {
349+
if (cur.val === val) {
350+
pre.next = cur.next;
351+
} else {
352+
pre = pre.next;
353+
}
354+
cur = cur.next;
355+
}
356+
return head.next;
357+
};
358+
```
359+
305360
Swift:
361+
306362
```swift
307363
/**
308364
* Definition for singly-linked list.

problems/链表理论基础.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,32 @@ public class ListNode {
168168
}
169169
```
170170

171+
JavaScript:
172+
173+
```javascript
174+
class ListNode {
175+
val;
176+
next = null;
177+
constructor(value) {
178+
this.val = value;
179+
this.next = null;
180+
}
181+
}
182+
```
183+
184+
TypeScript:
185+
186+
```typescript
187+
class ListNode {
188+
public val: number;
189+
public next: ListNode = null;
190+
constructor(value: number) {
191+
this.val = value;
192+
this.next = null;
193+
}
194+
}
195+
```
196+
171197
Python:
172198

173199

0 commit comments

Comments
 (0)