Skip to content

Commit c13e38e

Browse files
committed
update basic traversal algorithm
1 parent 7925242 commit c13e38e

File tree

1 file changed

+134
-10
lines changed

1 file changed

+134
-10
lines changed

数据结构篇/二叉树.md

Lines changed: 134 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ const buildTreeByArray = function (array, index) {
2525
let tn = null;
2626
if (index < array.length) {
2727
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);
28+
if (value !== null) {
29+
tn = new TreeNode(value);
30+
tn.left = buildTreeByArray(array, 2 * index + 1);
31+
tn.right = buildTreeByArray(array, 2 * index + 2);
32+
}
3133
return tn;
3234
}
3335
return tn;
@@ -45,7 +47,7 @@ let root = binaryTree(arr);
4547

4648
```js
4749
function preOrder(root) {
48-
if(root === null || root.val === null){
50+
if(root === null){
4951
return null;
5052
}
5153
console.log(root.val);
@@ -57,16 +59,32 @@ function preOrder(root) {
5759
##### 前序非递归
5860

5961
```js
60-
62+
var preOrderTraversal = function (root) {
63+
if(root === null){
64+
return [];
65+
}
66+
let res = [];
67+
let stack = [];
68+
stack.push(root);
69+
while(stack.length !== 0){
70+
let node = stack.pop();
71+
res.push(node.val);
72+
if (node.right !== null) {
73+
stack.push(node.right);
74+
}
75+
if (node.left !== null) {
76+
stack.push(node.left);
77+
}
78+
}
79+
return res;
80+
}
6181
```
6282

63-
64-
6583
##### 中序递归
6684

6785
```js
6886
function inOrder(root){
69-
if(root === null || root.val === null){
87+
if(root === null){
7088
return null;
7189
}
7290
inOrder(root.left);
@@ -75,11 +93,34 @@ function inOrder(root){
7593
}
7694
```
7795

96+
##### 中序非递归
97+
98+
```js
99+
var inOrderTraversal = function(root){
100+
if (root === null) {
101+
return [];
102+
}
103+
let res = [];
104+
let stack = [];
105+
let node = root;
106+
while(stack.length!==0 || node!==null){
107+
while(node !== null){
108+
stack.push(node);
109+
node = node.left;
110+
}
111+
node = stack.pop();
112+
res.push(node.val);
113+
node = node.right;
114+
}
115+
return res;
116+
}
117+
```
118+
78119
##### 后序递归
79120

80121
```js
81122
function postOrder(root){
82-
if(root === null || root.val === null){
123+
if(root === null){
83124
return null;
84125
}
85126
postOrder(root.left);
@@ -88,7 +129,90 @@ function postOrder(root){
88129
}
89130
```
90131

132+
##### 后序非递归
133+
134+
```js
135+
var postOrderTraversal = function(root){ //翻转非递归 后序遍历
136+
if (root === null) {
137+
return [];
138+
}
139+
let res = [];
140+
let stack = [];
141+
stack.push(root);
142+
while(stack.length !== 0){
143+
let node = stack.pop();
144+
res.push(node.val);
145+
if (node.left !== null) {
146+
stack.push(node.left);
147+
}
148+
if (node.right !== null) {
149+
stack.push(node.right);
150+
}
151+
}
152+
return res.reverse();
153+
}
154+
```
91155

156+
##### 深度遍历
157+
158+
```js
159+
var dfsUpToDown = function(root){ //递归,从上到下
160+
let res = [];
161+
dfs(root, res);
162+
return res;
163+
}
164+
165+
var dfs = function(node, res){
166+
if (node === null) {
167+
return null;
168+
}
169+
res.push(node.val);
170+
dfs(node.left, res);
171+
dfs(node.right, res);
172+
}
173+
174+
var dfsDownToUp = function(root){ //从下到上
175+
return divideAndConquer(root);
176+
}
177+
178+
var divideAndConquer = function(node){ // 分治法
179+
let res = [];
180+
if (node === null) {
181+
return null;
182+
}
183+
let left = divideAndConquer(node.left);
184+
let right = divideAndConquer(node.right);
185+
res.push(node.val);
186+
if (left !== null) {
187+
res = res.concat(left.flat());
188+
}
189+
if (right !== null) {
190+
res = res.concat(right.flat());
191+
}
192+
return res;
193+
}
194+
```
195+
196+
##### 广度遍历
197+
198+
```js
199+
var bfs = function(root){
200+
let res = [];
201+
let queue = [];
202+
queue.push(root);
203+
while(queue.length !== 0){
204+
let node = queue.shift();
205+
res.push(node.val);
206+
if (node.left !== null) {
207+
queue.push(node.left);
208+
}
209+
if (node.right !== null) {
210+
queue.push(node.right);
211+
}
212+
}
213+
return res;
214+
}
215+
```
92216

93217
##### 104.二叉树的最大深度[二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
94218

@@ -109,7 +233,7 @@ var maxDepth = function(root) { //递归
109233
if(root === null){
110234
return 0;
111235
}
112-
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
236+
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
113237
};
114238
```
115239

0 commit comments

Comments
 (0)