Skip to content

Commit ca18132

Browse files
committed
Create 94.二叉树的中序遍历.js
1 parent 2188843 commit ca18132

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

94.二叉树的中序遍历.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number[]}
11+
*/
12+
var inorderTraversal = function(root) {
13+
const result = [];
14+
const stack = [];
15+
let cur = root;
16+
while (cur !== null || stack.length !== 0) {
17+
while (cur !== null) {
18+
stack.push(cur);
19+
cur = cur.left;
20+
}
21+
cur = stack.pop();
22+
result.push(cur.val);
23+
cur = cur.right;
24+
}
25+
return result;
26+
};

0 commit comments

Comments
 (0)