zoukankan      html  css  js  c++  java
  • 树的各种遍历

    树的遍历

    1.前序遍历(preoOrder)

    https://leetcode.com/problems/binary-tree-preorder-traversal/

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def preorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            res = []
            node = []
            if not root:
                return res
            node.append(root)
            while node:
                temp = node.pop()
                res.append(temp.val)
                if temp.right:
                    node.append(temp.right)
                if temp.left:
                    node.append(temp.left)
            return res
    # 递归实现
    # return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) if root is not None else []
    
    

    2.中序遍历(inOrder)

    https://leetcode.com/problems/binary-tree-inorder-traversal/

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def inorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            node = []
            res = []
            if not root:
                return res
            node.append(root)
            temp = root
            while node:
                while temp and temp.left:
                    node.append(temp.left)
                    temp = temp.left
                temp = node.pop()
                res.append(temp.val)
                temp = temp.right
                if temp:
                    node.append(temp)
            return res
    # 递归实现
    #return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) if root is not None else []
    

    3.后序遍历(postOrder)

    https://leetcode.com/problems/binary-tree-postorder-traversal/

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def postorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            # 反的‘前序遍历’(根->右->左)
            node = []
            res = []
            if not root:
                return res
            node.append(root)
            temp = root
            while node:
                temp = node.pop()
                if temp:
                    res.append(temp.val)
                    node.append(temp.left)
                    node.append(temp.right)
            return res[::-1]
    # 递归求解
    # return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] if root is not None else []          
    

    4.层次遍历(Level Order Traversal)-从上到下

    https://leetcode.com/problems/binary-tree-level-order-traversal/

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def levelOrder(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if not root:
                return []
            result, level = [], [root]
            while level:
                result.append([node.val for node in level])
                temp = []
                for node in level:
                    temp.append(node.left)
                    temp.append(node.right)
                level = [node for node in temp if node]
            return result
    

    5.层次遍历(Level Order Traversal)-从下到上

    https://leetcode.com/problems/binary-tree-level-order-traversal-ii/

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def levelOrderBottom(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if not root:
                return []
            result, level = [], [root]
            while level:
                result.insert(0, [node.val for node in level])
                temp = []
                for node in level:
                    temp.append(node.left)
                    temp.append(node.right)
                level = [node for node in temp if node]
            return result
    

    6.之字形遍历(Zigzag Level Order Traversal)

    https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def zigzagLevelOrder(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if not root:
                return []
            result, level, direction = [], [root], 1
            while level:
                result.append([node.val for node in level][::direction])
                direction *= -1
                temp = []
                for node in level:
                    temp.append(node.left)
                    temp.append(node.right)
                level = [node for node in temp if node]
            return result
    
  • 相关阅读:
    django-rest-framework
    史上最全的状态码
    __new__、__init__、__call__三个特殊方法
    量化投资与Python之pandas
    MongoDB 3.0 Release Notes
    TokuMX写操作无法加锁的问题
    TokuMX唯一索引不支持dropDups选项
    接着写吧
    Windows下Python IDLE设置
    20145320周岐浩免考
  • 原文地址:https://www.cnblogs.com/binwone/p/6041606.html
Copyright © 2011-2022 走看看