forked from yechens/XiaoZhao-ChongChongChong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path94_二叉树的中序遍历.py
53 lines (46 loc) · 1.31 KB
/
94_二叉树的中序遍历.py
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
42
43
44
45
46
47
48
49
50
51
52
53
"""
题目描述:
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
解题思路:
递归遍历很简单;非递归解法用一个栈(数组)来保存遍历过程中的节点,且优先考虑左节点
中序遍历顺序:左 -> 根 -> 右
时间复杂度:O(n)
"""
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
if not root:
return []
# 1.非递归遍历
result = []
stack = [root]
node = root
while stack:
while node and node.left:
stack.append(node.left)
node = node.left
node = stack.pop()
result.append(node.val)
if node.right:
stack.append(node.right)
node = node.right
else:
node = None # 右节点不存在时要置空,否则会重复遍历
return result
# 递归遍历
def inOrder(root, res):
if not root:
return
inOrder(root.left, res)
res.append(root.val)
inOrder(root.right, res)
return res
return inOrder(root, [])