class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
'''先序遍历'''
#先序遍历(非递归法1)
def preOrder(root):
"""
:type root: TreeNode
:rtype: List[int]
"""
'''
思路分析:
preOrder每次都将遇到的节点压入栈,当左子树遍历完毕后才从栈中弹出最后一个访问的节点,访问其右子树。
在同一层中,不可能同时有两个节点压入栈,因此栈的大小空间为O(h),h为二叉树高度。
时间方面,每个节点都被压入栈一次,弹出栈一次,访问一次,复杂度为O(n)。
'''
if root == None:
return []
stack = []
result = []
while root or stack:
while root: # 从根节点开始,一直找它的左子树
result.append(root.val)
stack.append(root)
root = root.left # while结束表示当前节点为空,即前一个节点没有左子树了
root = stack.pop()
root = root.right # 开始查看它的右子树
return result
# 先序遍历(非递归法2)
def preOrder2(root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if root == None:
return []
stack = [root]
result = []
while stack:
result.append(root.val)
if root.right:
stack.append(root.right)
if root.left:
stack.append(root.left)
root = stack.pop()
return result
'''中序遍历'''
def inorder(root):
"""
:type root: TreeNode
:rtype: List[int]
"""
'''
思路分析(利用栈实现):
1. 使用一个栈保存结点(列表实现);
2. 如果结点存在,入栈,然后将当前指针指向左子树,直到为空;
3. 当前结点不存在,则出栈栈顶元素,并把当前指针指向栈顶元素的右子树;
4. 栈不为空,则循环步骤2、3。
'''
if root == None:
return []
stack = []
result = []
while root or stack:
if root:
stack.append(root)
root = root.left
else:
root = stack.pop()
result.append(root.val)
root = root.right
return result
'''后序遍历'''
def postOrder(root):
"""
:type root: TreeNode
:rtype: List[int]
"""
'''
思路分析:
后序遍历对比中序遍历难度要大一些。因为中序遍历节点序列有一点的连续性,
而后续遍历则感觉有一定的跳跃性。先左,再右,最后才中间节点;
访问左子树后,需要跳转到右子树,右子树访问完毕了再回溯至根节点并访问之。
'''
stack = [root]
stack2 = []
result = []
while len(stack) > 0:
root = stack.pop()
stack2.append(root)
if root.left:
stack.append(root.left)
if root.right:
stack.append(root.right)
while len(stack2) > 0:
node = stack2.pop()
result.append(node.val)
return result
'''层次遍历'''
# 层次遍历法1:先进后出选用栈结构
def levelOrder(root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root == None:
return []
result = []
level = []
now_list = [root]
next_list = []
while len(now_list) != 0:
while len(now_list) != 0:
temp = now_list.pop(0)
level.append(temp.val)
if temp.left:
next_list.append(temp.left)
if temp.right:
next_list.append(temp.right)
result.append(level)
now_list, next_list = next_list, []
level = []
return result
# 层次遍历法2:先进先出选用队列结构
import queue
def levelOrder2(root):
if root == None:
return []
level = []
result = []
now_que = queue.Queue() # 创建先进先出队列
next_que = queue.Queue()
now_que.put(root)
while not now_que.empty():
while not now_que.empty():
root = now_que.get()
level.append(root.val)
if root.left: # 若该节点存在左子节点,则加入队列(先push左节点)
next_que.put(root.left)
if root.right: # 若该节点存在右子节点,则加入队列(再push右节点)
next_que.put(root.right)
result.append(level)
now_que = next_que
next_que = queue.Queue()
level = []
return result
'''Test---[1,6,2,3]'''
testTree = TreeNode(1)
root = testTree
root.left = TreeNode(6)
root.right = TreeNode(2)
root = root.right
root.left = TreeNode(3)
root = testTree # 返回到根节点
print('先序遍历法1:',preOrder(root))
print('先序遍历法2:',preOrder2(root))
print('中序遍历:',inorder(root))
print('后序遍历:',postOrder(root))
print('层次遍历法1:',levelOrder(root))
print('层次遍历法2:',levelOrder2(root))