@@ -25,9 +25,11 @@ const buildTreeByArray = function (array, index) {
25
25
let tn = null ;
26
26
if (index < array .length ) {
27
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 );
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
+ }
31
33
return tn;
32
34
}
33
35
return tn;
@@ -45,7 +47,7 @@ let root = binaryTree(arr);
45
47
46
48
``` js
47
49
function preOrder (root ) {
48
- if (root === null || root . val === null ){
50
+ if (root === null ){
49
51
return null ;
50
52
}
51
53
console .log (root .val );
@@ -57,16 +59,32 @@ function preOrder(root) {
57
59
##### 前序非递归
58
60
59
61
``` 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
+ }
61
81
```
62
82
63
-
64
-
65
83
##### 中序递归
66
84
67
85
``` js
68
86
function inOrder (root ){
69
- if (root === null || root . val === null ){
87
+ if (root === null ){
70
88
return null ;
71
89
}
72
90
inOrder (root .left );
@@ -75,11 +93,34 @@ function inOrder(root){
75
93
}
76
94
```
77
95
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
+
78
119
##### 后序递归
79
120
80
121
``` js
81
122
function postOrder (root ){
82
- if (root === null || root . val === null ){
123
+ if (root === null ){
83
124
return null ;
84
125
}
85
126
postOrder (root .left );
@@ -88,7 +129,90 @@ function postOrder(root){
88
129
}
89
130
```
90
131
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
+ ```
91
155
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
+ ```
92
216
93
217
##### 104.二叉树的最大深度[ 二叉树的最大深度] ( https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ )
94
218
@@ -109,7 +233,7 @@ var maxDepth = function(root) { //递归
109
233
if (root === null ){
110
234
return 0 ;
111
235
}
112
- return Math .max (maxDepth (root .left ), maxDepth (root .right ))+ 1 ;
236
+ return Math .max (maxDepth (root .left ), maxDepth (root .right ))+ 1 ;
113
237
};
114
238
```
115
239
0 commit comments