1. 程式人生 > >python遍歷二叉樹

python遍歷二叉樹

elf class [] res 兩個棧 樹節點 roo pri pytho

定義二叉樹:

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

構建二叉樹:

# 返回構造的TreeNode根節點
    def reConstructBinaryTree(self, pre, tin):
        if not pre or not tin:
            return None
        root = TreeNode(pre[0])#根節點
        # 判斷輸入的兩個序列是不是匹配
if set(pre) != set(tin): return None i = tin.index(root.val) # i == 3 root.left = self.reConstructBinaryTree(pre[1:i+1],tin[:i]) # 列表:左閉右開 root.right = self.reConstructBinaryTree(pre[i+1:],tin[i+1:]) return root

BFS:

def BFS(self, root):   #
寬度優先遍歷BFS array = [] result = [] if root == None: return result array.append(root) while array: newNode = array.pop(0) # 根結點 result.append(newNode.val) if newNode.left != None: array.append(newNode.left) if newNode.right != None: array.append(newNode.right)
return result



先序遍歷:
1.遞歸版本:
def pre_traversal(self):
        ret = []

        def traversal(head):
            if not head:
                return
            ret.append(head.val)
            traversal(head.left)
            traversal(head.right)

        traversal(self.root)
        return ret

2.非遞歸版本

# 先序打印二叉樹(非遞歸)
def preOrderTravese(node):
    stack = [node]
    while len(stack) > 0:
        print(node.val)
        if node.right is not None:
            stack.append(node.right)
        if node.left is not None:
            stack.append(node.left)
        node = stack.pop()

中序遍歷:

1.遞歸版本

def in_traversal(self):
        ret = []

        def traversal(head):
            if not head:
                return
            traversal(head.left)
            ret.append(head.val)
            traversal(head.right)

        traversal(self.root)
        return ret

2.非遞歸版本

# 中序打印二叉樹(非遞歸)
def inOrderTraverse(node):
    stack = []
    pos = node
    while pos is not None or len(stack) > 0:
        if pos is not None:
            stack.append(pos)
            pos = pos.left
        else:
            pos = stack.pop()
            print(pos.val)
            pos = pos.right

後序遍歷:

1.遞歸版本

def post_traversal(self):
        ret = []

        def traversal(head):
            if not head:
                return
            traversal(head.left)
            traversal(head.right)
            ret.append(head.val)

        traversal(self.root)
        return ret

2.非遞歸版本

# 後序打印二叉樹(非遞歸)
# 使用兩個棧結構
# 第一個棧進棧順序:左節點->右節點->跟節點
# 第一個棧彈出順序: 跟節點->右節點->左節點(先序遍歷棧彈出順序:跟->左->右)
# 第二個棧存儲為第一個棧的每個彈出依次進棧
# 最後第二個棧依次出棧
def postOrderTraverse(node):
    stack = [node]
    stack2 = []
    while len(stack) > 0:
        node = stack.pop()
        stack2.append(node)
        if node.left is not None:
            stack.append(node.left)
        if node.right is not None:
            stack.append(node.right)
    while len(stack2) > 0:
        print(stack2.pop().val)

求二叉樹最大深度:

# 二叉樹的最大深度
def bTreeDepth(node):
    if node is None:
        return 0
    ldepth = bTreeDepth(node.left)
    rdepth = bTreeDepth(node.right)
    return (max(ldepth, rdepth) + 1)

求二叉樹節點個數:

# 求二叉樹節點個數
def treeNodenums(node):
    if node is None:
        return 0
    nums = treeNodenums(node.left)
    nums += treeNodenums(node.right)
    return nums + 1




python遍歷二叉樹