LeetCodeAnimation/notes/LeetCode第94号问题:二叉树的中序遍历.md
2019-05-02 16:23:13 +08:00

69 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# LeetCode 94 号问题二叉树的中序遍历
> 本文首发于公众号五分钟学算法[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
>
> 个人网站[https://www.cxyxiaowu.com](https://www.cxyxiaowu.com)
题目来源于 LeetCode 上第 94 号问题二叉树的中序遍历题目难度为 Medium目前通过率为 35.8%
### 题目描述
给定一个二叉树返回它的*中序* 遍历
**示例:**
```
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
```
**进阶:** 递归算法很简单你可以通过迭代算法完成吗
### 题目解析
**(Stack)**的思路来处理问题
中序遍历的顺序为**--**具体算法为
- 从根节点开始先将根节点压入栈
- 然后再将其所有左子结点压入栈取出栈顶节点保存节点值
- 再将当前指针移到其右子节点上若存在右子节点则在下次循环时又可将其所有左子结点压入栈中
### 动画描述
![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190502102629.gif)
### 代码实现
```
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
return list;
}
}
```
![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/blog/fz0rq.png)