Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb1a92a

Browse files
author
luzhipeng
committed
中序遍历
1 parent 078248e commit eb1a92a

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

94.binary-tree-inorder-traversal.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
## 题目地址
2+
https://leetcode.com/problems/binary-tree-inorder-traversal/description/
3+
4+
## 题目描述
5+
Given a binary tree, return the inorder traversal of its nodes' values.
6+
7+
Example:
8+
9+
Input: [1,null,2,3]
10+
1
11+
\
12+
2
13+
/
14+
3
15+
16+
Output: [1,3,2]
17+
Follow up: Recursive solution is trivial, could you do it iteratively?
18+
19+
## 思路
20+
21+
递归的方式相对简单,非递归的方式借助栈这种数据结构实现起来会相对轻松。
22+
23+
如果采用非递归,可以用栈(Stack)的思路来处理问题。
24+
25+
中序遍历的顺序为左-根-右,具体算法为:
26+
27+
- 从根节点开始,先将根节点压入栈
28+
29+
- 然后再将其所有左子结点压入栈,取出栈顶节点,保存节点值
30+
31+
- 再将当前指针移到其右子节点上,若存在右子节点,则在下次循环时又可将其所有左子结点压入栈中, 重复上步骤
32+
33+
![94.binary-tree-inorder-traversal](./assets/94.binary-tree-inorder-traversal.gif)
34+
35+
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
36+
## 关键点解析
37+
38+
- 二叉树的基本操作(遍历)
39+
> 不同的遍历算法差异还是蛮大的
40+
- 如果非递归的话利用栈来简化操作
41+
42+
- 如果数据规模不大的话,建议使用递归
43+
44+
- 递归的问题需要注意两点,一个是终止条件,一个如何缩小规模
45+
46+
1. 终止条件,自然是当前这个元素是null(链表也是一样)
47+
48+
2. 由于二叉树本身就是一个递归结构, 每次处理一个子树其实就是缩小了规模,
49+
难点在于如何合并结果,这里的合并结果其实就是`left.concat(mid).concat(right)`,
50+
mid是一个具体的节点,left和right`递归求出即可`
51+
52+
53+
## 代码
54+
55+
```js
56+
/*
57+
* @lc app=leetcode id=94 lang=javascript
58+
*
59+
* [94] Binary Tree Inorder Traversal
60+
*
61+
* https://leetcode.com/problems/binary-tree-inorder-traversal/description/
62+
*
63+
* algorithms
64+
* Medium (55.22%)
65+
* Total Accepted: 422.4K
66+
* Total Submissions: 762.1K
67+
* Testcase Example: '[1,null,2,3]'
68+
*
69+
* Given a binary tree, return the inorder traversal of its nodes' values.
70+
*
71+
* Example:
72+
*
73+
*
74+
* Input: [1,null,2,3]
75+
* ⁠ 1
76+
* ⁠ \
77+
* ⁠ 2
78+
* ⁠ /
79+
* ⁠ 3
80+
*
81+
* Output: [1,3,2]
82+
*
83+
* Follow up: Recursive solution is trivial, could you do it iteratively?
84+
*
85+
*/
86+
/**
87+
* Definition for a binary tree node.
88+
* function TreeNode(val) {
89+
* this.val = val;
90+
* this.left = this.right = null;
91+
* }
92+
*/
93+
/**
94+
* @param {TreeNode} root
95+
* @return {number[]}
96+
*/
97+
var inorderTraversal = function(root) {
98+
// 1. Recursive solution
99+
// if (!root) return [];
100+
// const left = root.left ? inorderTraversal(root.left) : [];
101+
// const right = root.right ? inorderTraversal(root.right) : [];
102+
// return left.concat([root.val]).concat(right);
103+
104+
// 2. iterative solutuon
105+
if (!root) return [];
106+
const stack = [root];
107+
const ret = [];
108+
let left = root.left;
109+
110+
let item = null; // stack 中弹出的当前项
111+
112+
while(left) {
113+
stack.push(left);
114+
left = left.left;
115+
}
116+
117+
while(item = stack.pop()) {
118+
ret.push(item.val);
119+
let t = item.right;
120+
121+
while(t) {
122+
stack.push(t);
123+
t = t.left;
124+
}
125+
}
126+
127+
return ret;
128+
129+
};
130+
131+
```
132+
370 KB
Loading

0 commit comments

Comments
 (0)