# Heading
94.二叉树的中序遍历 (opens new window)
Tags: algorithms microsoft hash-table stack tree
Langs: c cpp csharp golang java javascript kotlin php python python3 ruby rust scala swift typescript
- algorithms
- Medium (73.85%)
- Likes: 755
- Dislikes: -
- Total Accepted: 288.2K
- Total Submissions: 389.8K
- Testcase Example: '[1,null,2,3]'
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
/*
* @lc app=leetcode.cn id=94 lang=javascript
*
* [94] 二叉树的中序遍历
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
//递归
var inorderTraversal = function (root) {
if (root === null) return []
return [...inorderTraversal(root.left), root.val, ...inorderTraversal(root.right)]
};
var inorderTraversal = function (root) {
let ret = [],
stack = [], p = root;
while (p || stack.length) {
if (p) {
stack.push(p);
p = p.left;
} else {
p = stack.pop();
ret.push(p.val);
p = p.right;
}
}
return ret;
};
// @lc code=end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41