1 问题描述
给定一个二叉树,按照左,中,右的顺序遍历这棵树。
2 代码实现
思路很简单,从左到右遍历这颗二叉树即可。
2.1 递归代码实现
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
# 中序遍历顺序:左子树->根节点->右子树
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
out = []
if root is not None:
if root.left is not None:
out.extend(self.inorderTraversal(root.left))
out.append(root.val)
if root.right is not None:
out.extend(self.inorderTraversal(root.right))
return out
时间复杂度 O ( n ) O(n) O(n),如果是单链,空间复杂度就是 O ( n ) O(n) O(n)。
2.2 迭代法实现
迭代法思想与递归相同,但是需要手动堆栈,按照栈先进后出的规则,很容易实现:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
# 中序遍历顺序:左子树->根节点->右子树
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
out = []
if root is not None:
dqueue = [0]
len_dq = 0
while 1:
while root is not None:
dqueue.append(root)
len_dq += 1
root = root.left
root = dqueue.pop()
len_dq -= 1
out.append(root.val)
root = root.right
if ((len_dq < 1) & (root is None)):
break
return out
时间复杂度 O ( n ) O(n) O(n),空间复杂度是 O ( n ) O(n) O(n)。
2.3 Morris 中序遍历
这个是真不会,完全看官方题解才知道有这种思想,主要作用是将空间复杂大降到 O ( 1 ) O(1) O(1),但要遍历一个节点好两次。
直接copy Leetcode官方的解说:
Morris 遍历算法整体步骤如下(假设当前遍历到的节点为 x x x):
- 如果 x x x 无左孩子,先将 x x x 的值加入答案数组,再访问 x x x的右孩子,即 x = x . r i g h t x= x.right x=x.right.
- 如果
x
x
x 有左孩子,则找到
x
x
x 左子树上最右的节点(即左子树中序遍历的最后一个节点,
x
x
x 在中序遍历中的前驱节点),我们记为
p
r
e
d
e
c
e
s
s
o
r
predecessor
predecessor。根据
p
r
e
d
e
c
e
s
s
o
r
predecessor
predecessor的右孩子是否为空,进行如下操作:
(1) 如果 p r e d e c e s s o r predecessor predecessor的右孩子为空,则将其右孩子指向 x x x,然后令 p r e d e c e s s o r . r i g h t = x predecessor.right= x predecessor.right=x,然后访问 x x x 的左孩子,即 x = x . l e f t x=x.left x=x.left。
(2) 如果 p r e d e c e s s o r predecessor predecessor 的右孩子不为空,则此时其右孩子指向 x x x,说明我们已经遍历完 x x x 的左子树,我们将 p r e d e c e s s o r predecessor predecessor 的右孩子置空,将 x x x 的值加入答案数组,然后访问 x x x 的右孩子,即 x = x . r i g h t x= x.right x=x.right。 - 重复上述操作,直至访问完整棵树。
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/er-cha-shu-de-zhong-xu-bian-li-by-leetcode-solutio/
来源:力扣(LeetCode)
其实思想并不难,还是优先遍历这棵树的左子树,不过将其最后遍历的最右子树连接到了根节点,以此来避免堆栈,注意这种结构并不会导致树的原始结构改变,但很可能根节点的地址找不到了。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
# 中序遍历顺序:左子树->根节点->右子树
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
out = []
if root is not None:
while root:
if root.left is None:
out.append(root.val)
root = root.right
else:
predecessor = root.left
while ((predecessor.right is not None) & (predecessor.right != root)):
predecessor = predecessor.right
if predecessor.right:
out.append(root.val)
predecessor.right = None
root = root.right
else:
predecessor.right = root
root = root.left
return out
时间复杂度 O ( n ) O(n) O(n),空间复杂度 O ( 1 ) O(1) O(1)。