Skip to content

Commit

Permalink
feat: 中序遍历
Browse files Browse the repository at this point in the history
  • Loading branch information
robot committed Dec 29, 2021
1 parent a9018be commit d697340
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 72 deletions.
97 changes: 25 additions & 72 deletions problems/94.binary-tree-inorder-traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,19 @@ https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
JavaScript Code:

```js
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function (root) {
// 1. Recursive solution
// if (!root) return [];
// const left = root.left ? inorderTraversal(root.left) : [];
// const right = root.right ? inorderTraversal(root.right) : [];
// return left.concat([root.val]).concat(right);

// 2. iterative solutuon
if (!root) return [];
const stack = [root];
const ret = [];
let left = root.left;

let item = null; // stack 中弹出的当前项

while (left) {
stack.push(left);
left = left.left;
}

while ((item = stack.pop())) {
ret.push(item.val);
let t = item.right;

while (t) {
stack.push(t);
t = t.left;
const res = [];
const stk = [];
while (root || stk.length) {
while (root) {
stk.push(root);
root = root.left;
}
root = stk.pop();
res.push(root.val);
root = root.right;
}

return ret;
return res;
};
```

Expand Down Expand Up @@ -143,46 +121,22 @@ public:
Python Code:
```Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
```py
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
"""
1. 递归法可以一行代码完成,无需讨论;
2. 迭代法一般需要通过一个栈保存节点顺序,我们这里直接使用列表
- 首先,我要按照中序遍历的顺序存入栈,这边用的逆序,方便从尾部开始处理
- 在存入栈时加入一个是否需要深化的参数
- 在回头取值时,这个参数应该是否,即直接取值
- 简单调整顺序,即可实现前序和后序遍历
"""
# 递归法
# if root is None:
# return []
# return self.inorderTraversal(root.left)\
# + [root.val]\
# + self.inorderTraversal(root.right)
# 迭代法
result = []
stack = [(1, root)]
while stack:
go_deeper, node = stack.pop()
if node is None:
continue
if go_deeper:
# 左右节点还需继续深化,并且入栈是先右后左
stack.append((1, node.right))
# 节点自身已遍历,回头可以直接取值
stack.append((0, node))
stack.append((1, node.left))
else:
result.append(node.val)
return result
if not root: return []
stack = []
ans = []
cur = root
while cur or stack:
while cur:
stack.append(cur)
cur = cur.left
cur = stack.pop()
ans.append(cur.val)
cur = cur.right
return ans
```

Java Code:
Expand Down Expand Up @@ -253,7 +207,6 @@ class Solution {

- [二叉树的遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md)


大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
4 changes: 4 additions & 0 deletions thinkings/binary-tree-traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ def MorrisTraversal(root):

**划重点:Morris 是一种可以在 $O(1)$ 空间遍历二叉树的算法。\***

## 相关专题

- [几乎刷完了力扣所有的树题,我发现了这些东西。。。](https://lucifer.ren/blog/2020/11/23/tree/)

## 相关题目

- [lowest-common-ancestor-of-a-binary-tree](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
Expand Down

0 comments on commit d697340

Please sign in to comment.