1. 程式人生 > 實用技巧 >threading的定時器模組,python,每間隔一段時間執行一次任務

threading的定時器模組,python,每間隔一段時間執行一次任務

二叉樹遍歷

樹的遍歷是樹的一種重要的運算。所謂遍歷是指對樹中所有結點的資訊的訪問,即依次對樹中每個結點訪問一次且僅訪問一次,我們把這種對所有節點的訪問稱為遍歷(traversal)。那麼樹的兩種重要的遍歷模式是深度優先遍歷和廣度優先遍歷,深度優先一般用遞迴,廣度優先一般用佇列。一般情況下能用遞迴實現的演算法大部分也能用堆疊來實現。

深度優先遍歷

對於一顆二叉樹,深度優先搜尋(Depth First Search)是沿著樹的深度遍歷樹的節點,儘可能深的搜尋樹的分支。
那麼深度遍歷有重要的三種方法。這三種方式常被用於訪問樹的節點,它們之間的不同在於訪問每個節點的次序不同。這三種遍歷分別叫做先序遍歷(preorder),中序遍歷(inorder)和後序遍歷(postorder)。我們來給出它們的詳細定義,然後舉例看看它們的應用。

  • 先序遍歷 在先序遍歷中,我們先訪問根節點,然後遞迴使用先序遍歷訪問左子樹,再遞迴使用先序遍歷訪問右子樹
    根節點->左子樹->右子樹
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: The root of binary tree.
    @return: Postorder in ArrayList which contains node values.
    
""" result = [] def preorderTraversal(self, root): # write your code here if root is None: return [] stack = [] seq = [] #記錄先序訪問序列 while ((root!=None) | (len(stack)!=0)): if root!=None: seq.append(root.val) #先訪問根節點
stack.append(root) root = root.left else: root = stack.pop() #回溯至父節點 root = root.right return seq
  • 中序遍歷 在中序遍歷中,我們遞迴使用中序遍歷訪問左子樹,然後訪問根節點,最後再遞迴使用中序遍歷訪問右子樹

   左子樹->根節點->右子樹

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: The root of binary tree.
    @return: Postorder in ArrayList which contains node values.
    """
    result = []
    def inorderTraversal(self, root):
        # write your code here
        if root is None:
            return []
        stack = []
        seq = []
        output = []
        while ((root!=None) | (len(stack)!=0)):
            if root!=None:
                stack.append(root)
                root = root.left
            else:
                root = stack.pop()
                seq.append(root.val) # 左孩子先pop出來,再pop根節點
                root = root.right
         
        return seq     
  • 後序遍歷 在後序遍歷中,我們先遞迴使用後序遍歷訪問左子樹和右子樹,最後訪問根節點
    左子樹->右子樹->根節點

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: The root of binary tree.
    @return: Postorder in ArrayList which contains node values.
    """
    result = []
    def postorderTraversal(self, root):
        # write your code here
        if root is None:
            return []
        stack = []
        seq = []
        output = []
        while ((root!=None) | (len(stack)!=0)):
            if root!=None:
                seq.append(root.val)
                stack.append(root)
                root = root.right  # 這從left變成了 right
            else:
                root = stack.pop()
                root = root.left # 這從right變成了 left
                
        while seq:  # 後序遍歷 是 將先序遍歷的反過來
            output.append(seq.pop())

        return output                    

廣度優先遍歷(層次遍歷)

從樹的root開始,從上到下從從左到右遍歷整個樹的節點



"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: The root of binary tree.
    @return: Postorder in ArrayList which contains node values.
    """
    def BFS(self, root):  # 層次遍歷核心程式碼
        if root == None:
            return
        queue = []
        res = []
        queue.append(root)

        while queue:
            now_node = queue.pop(0)
            res.append(now_node.val)

            if now_node.left != None:
                queue.append(now_node.left)

            if now_node.right != None:
                queue.append(now_node.right)

        return res