Skip to content

Commit 7925242

Browse files
committed
update tree structure
1 parent 8f105fb commit 7925242

File tree

3 files changed

+103
-5
lines changed

3 files changed

+103
-5
lines changed

基础算法篇/二分搜索.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#### 二分搜索模板
44

5-
给一个**有序数组**和目标值,找第一次/最后一次/任何一次出现的索引,如果没有出现返回-1
5+
给一个**有序数组**和目标值,找第一次/最后一次/任何一次出现的索引,如果没有出现返回 -1
66

77
模板四点要素
88

@@ -20,7 +20,7 @@
2020
var search = function(nums, target) {
2121
let left = 0, mid = 0, right = nums.length-1;
2222
while(left + 1 < right){
23-
mid = left + ((right-left) >> 2);
23+
mid = left + ((right-left) >> 1); // 考虑到 start + end 数据溢出的情况
2424
if(nums[mid] === target){
2525
return mid;
2626
}
@@ -71,7 +71,7 @@ var search = function(nums, target) {
7171
var search = function(nums, target) {
7272
let start = 0, mid = 0, end = nums.length-1;
7373
while(start <= end){
74-
mid = start + ((start+end)>>2);
74+
mid = start + ((end-start)>>1);
7575
if(nums[mid] === target){
7676
return mid;
7777
}

数据结构篇/二叉树.md

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,95 @@
11
### 二叉树
22

3+
#### 二叉树遍历
4+
5+
**前序遍历****先访问根节点**,再前序遍历左子树,再前序遍历右子树 **中序遍历**:先中序遍历左子树,**再访问根节点**,再中序遍历右子树 **后序遍历**:先后序遍历左子树,再后序遍历右子树,**再访问根节点**
6+
7+
注意点:
8+
9+
- 以根访问顺序决定是什么遍历
10+
- 左子树都是优先右子树
11+
12+
##### 树结构
13+
14+
```js
15+
function TreeNode(val){
16+
this.val = val;
17+
this.left = this.right = null;
18+
}
19+
```
20+
21+
##### 根据数组构建二叉树
22+
23+
```js
24+
const buildTreeByArray = function (array, index) {
25+
let tn = null;
26+
if (index < array.length) {
27+
const value = array[index];
28+
tn = new TreeNode(value);
29+
tn.left = buildTreeByArray(array, 2 * index + 1);
30+
tn.right = buildTreeByArray(array, 2 * index + 2);
31+
return tn;
32+
}
33+
return tn;
34+
}
35+
36+
const binaryTree = function (array) {
37+
return buildTreeByArray(array, 0);
38+
}
39+
40+
let arr = [1,2,3,null,4,5,null,null,null,6,7];
41+
let root = binaryTree(arr);
42+
```
43+
44+
##### 前序递归
45+
46+
```js
47+
function preOrder(root) {
48+
if(root === null || root.val === null){
49+
return null;
50+
}
51+
console.log(root.val);
52+
preOrder(root.left);
53+
preOrder(root.right);
54+
}
55+
```
56+
57+
##### 前序非递归
58+
59+
```js
60+
61+
```
62+
63+
64+
65+
##### 中序递归
66+
67+
```js
68+
function inOrder(root){
69+
if(root === null || root.val === null){
70+
return null;
71+
}
72+
inOrder(root.left);
73+
console.log(root.val);
74+
inOrder(root.right);
75+
}
76+
```
77+
78+
##### 后序递归
79+
80+
```js
81+
function postOrder(root){
82+
if(root === null || root.val === null){
83+
return null;
84+
}
85+
postOrder(root.left);
86+
postOrder(root.right);
87+
console.log(root.val);
88+
}
89+
```
90+
91+
92+
393
##### 104.二叉树的最大深度[二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
494

595
给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。**说明:** 叶子节点是指没有子节点的节点。
@@ -9,7 +99,7 @@
999
> 3
10100
> / \
11101
> 9 20
12-
> / \
102+
> / \
13103
> 15 7
14104
>
15105
> 返回它的最大深度 3 。
@@ -19,7 +109,7 @@ var maxDepth = function(root) { //递归
19109
if(root === null){
20110
return 0;
21111
}
22-
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
112+
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
23113
};
24114
```
25115

数据结构篇/链表.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
> 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
1919
2020
```js
21+
/**
22+
* Definition for singly-linked list.
23+
* function ListNode(val) {
24+
* this.val = val;
25+
* this.next = null;
26+
* }
27+
*/
28+
2129
var deleteDuplicates = function(head) {
2230
let current = head;
2331
while(current !== null && current.next !== null){

0 commit comments

Comments
 (0)