Skip to content

Commit 30bb486

Browse files
committed
Add js solution to leetcode no.94
1 parent f78e6d9 commit 30bb486

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,43 @@ class Solution {
204204
}
205205
}
206206
```
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+
```
207244

208245
### **...**
209246

solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,44 @@ class Solution {
189189
}
190190
}
191191
```
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+
192230

193231
### **...**
194232

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
};

0 commit comments

Comments
 (0)