這裏先對二叉樹的形式做個定義:
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
二叉樹的四種遍歷方式:
a
/ \
b c
/ \
d e
- 先序遍歷:每次遍歷樹時先訪問當前節點,再按照這種方式依次遍歷左子樹和右子樹。上圖遍歷結果爲:abdec
- 中序遍歷:每次遍歷樹時先遍歷左子樹,再訪問當前節點,最後遍歷右子樹。上圖遍歷結果爲:dbeac
- 後序遍歷:每次遍歷樹時先遍歷左子樹,再遍歷右子樹,最後訪問當前節點。上圖遍歷結果爲:debca
- 層序遍歷:按照從上往下,從左到右的順序遍歷。上圖遍歷結果爲:abcde
遞歸寫法
# -*- coding: UTF-8 -*-
from collections import deque
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class Tree:
def __init__(self):
self.root = None
def show(self):
return
def construct(self, li = None):
if not li:
return None
tl = []
for i in li:
if i is None:
tl.append(None)
else:
tl.append(TreeNode(i))
for idx in range(len(li) / 2):
if idx * 2 + 1 < len(tl) and tl[idx * 2 + 1]:
tl[idx].left = tl[idx * 2 + 1]
if idx * 2 + 2 < len(tl) and tl[idx * 2 + 2]:
tl[idx].right = tl[idx * 2 + 2]
self.root = tl[0]
def preOrder(self, cur):
if not cur:
return
print(cur.val)
self.preOrder(cur.left)
self.preOrder(cur.right)
def inOrder(self, cur):
if not cur:
return
self.inOrder(cur.left)
print(cur.val)
self.inOrder(cur.right)
def postOrder(self, cur):
if not cur:
return
self.postOrder(cur.left)
self.postOrder(cur.right)
print(cur.val)
def levelOrder(self, cur):
dq = deque()
dq.append(cur)
while dq:
tmp = dq.popleft()
if not tmp:
continue
print(tmp.val)
dq.append(tmp.left)
dq.append(tmp.right)
l = [2, 3, 4, 5, None, 7]
t = Tree()
t.construct(l)
print("pre order:")
t.preOrder(t.root)
print("in order:")
t.inOrder(t.root)
print("post order:")
t.postOrder(t.root)
print("level order:")
t.levelOrder(t.root)
非遞歸寫法
在遞歸寫法中,通過函數調用棧保存了程序的中間數據。非遞歸寫法則必須藉助一個輔助的棧:
# -*- coding: UTF-8 -*-
from collections import deque
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class Tree:
def __init__(self):
self.root = None
def show(self):
return
def construct(self, li = None):
if not li:
return None
tl = []
for i in li:
if i is None:
tl.append(None)
else:
tl.append(TreeNode(i))
for idx in range(len(li) / 2):
if idx * 2 + 1 < len(tl) and tl[idx * 2 + 1]:
tl[idx].left = tl[idx * 2 + 1]
if idx * 2 + 2 < len(tl) and tl[idx * 2 + 2]:
tl[idx].right = tl[idx * 2 + 2]
self.root = tl[0]
def preOrder(self, cur):
if not cur:
return
stack = []
stack.append(cur)
while stack:
cur = stack.pop()
if not cur:
continue
print(cur.val)
stack.append(cur.right)
stack.append(cur.left)
def inOrder(self, cur):
if not cur:
return
stack = []
while stack or cur:
while cur:
stack.append(cur)
cur = cur.left
cur = stack.pop()
print(cur.val)
cur = cur.right
def postOrder(self, cur):
if not cur:
return
stack = []
stack2 = []
while stack2 or cur:
while cur:
stack.append(cur)
stack2.append(cur)
cur = cur.right
cur = stack2.pop()
cur = cur.left
while stack:
cur = stack.pop()
print(cur.val)
def levelOrder(self, cur):
dq = deque()
dq.append(cur)
while dq:
tmp = dq.popleft()
if not tmp:
continue
print(tmp.val)
dq.append(tmp.left)
dq.append(tmp.right)
l = [2, 3, 4, 5, None, 7]
t = Tree()
t.construct(l)
print("pre order:")
t.preOrder(t.root)
print("in order:")
t.inOrder(t.root)
print("post order:")
t.postOrder(t.root)
print("level order:")
t.levelOrder(t.root)