File tree 3 files changed +105
-0
lines changed
solution/0000-0099/0094.Binary Tree Inorder Traversal
3 files changed +105
-0
lines changed Original file line number Diff line number Diff line change @@ -204,6 +204,43 @@ class Solution {
204
204
}
205
205
}
206
206
```
207
+ ### ** JavaScript**
208
+
209
+ 递归:
210
+
211
+ ``` js
212
+ var inorderTraversal = function (root ) {
213
+ let res = [];
214
+ function inorder (root ){
215
+ if (root){
216
+ inorder (root .left );
217
+ res .push (root .val );
218
+ inorder (root .right );
219
+ }
220
+ }
221
+ inorder (root);
222
+ return res;
223
+ };
224
+ ```
225
+ 非递归:
226
+
227
+ ``` js
228
+ var inorderTraversal = function (root ) {
229
+ let res = [], stk = [];
230
+ let cur = root;
231
+ while (cur || stk .length !== 0 ) {
232
+ while (cur) {
233
+ stk .push (cur);
234
+ cur = cur .left ;
235
+ }
236
+ let top = stk .pop ();
237
+ res .push (top .val );
238
+ cur = top .right ;
239
+
240
+ }
241
+ return res;
242
+ };
243
+ ```
207
244
208
245
### ** ...**
209
246
Original file line number Diff line number Diff line change @@ -189,6 +189,44 @@ class Solution {
189
189
}
190
190
}
191
191
```
192
+ ### ** JavaScript**
193
+
194
+ Recursive:
195
+
196
+ ``` js
197
+ var inorderTraversal = function (root ) {
198
+ let res = [];
199
+ function inorder (root ){
200
+ if (root){
201
+ inorder (root .left );
202
+ res .push (root .val );
203
+ inorder (root .right );
204
+ }
205
+ }
206
+ inorder (root);
207
+ return res;
208
+ };
209
+ ```
210
+ Non-recursive:
211
+
212
+ ``` js
213
+ var inorderTraversal = function (root ) {
214
+ let res = [], stk = [];
215
+ let cur = root;
216
+ while (cur || stk .length !== 0 ) {
217
+ while (cur) {
218
+ stk .push (cur);
219
+ cur = cur .left ;
220
+ }
221
+ let top = stk .pop ();
222
+ res .push (top .val );
223
+ cur = top .right ;
224
+
225
+ }
226
+ return res;
227
+ };
228
+ ```
229
+
192
230
193
231
### ** ...**
194
232
Original file line number Diff line number Diff line change
1
+ //non-recursive
2
+ var inorderTraversal = function ( root ) {
3
+ let res = [ ] , stk = [ ] ;
4
+ let cur = root ;
5
+ while ( cur || stk . length !== 0 ) {
6
+ while ( cur ) {
7
+ stk . push ( cur ) ;
8
+ cur = cur . left ;
9
+ }
10
+ let top = stk . pop ( ) ;
11
+ res . push ( top . val ) ;
12
+ cur = top . right ;
13
+
14
+ }
15
+ return res ;
16
+ } ;
17
+
18
+ //recursive
19
+ var inorderTraversal = function ( root ) {
20
+ let res = [ ] ;
21
+ function inorder ( root ) {
22
+ if ( root ) {
23
+ inorder ( root . left ) ;
24
+ res . push ( root . val ) ;
25
+ inorder ( root . right ) ;
26
+ }
27
+ }
28
+ inorder ( root ) ;
29
+ return res ;
30
+ } ;
You can’t perform that action at this time.
0 commit comments