【Leetcode】树的遍历(前、中、后、层序)

2020-07-20  本文已影响0人  李白开水

前序遍历

144. 二叉树的前序遍历

递归:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        return self.helper(root, [])

    def helper(self, node, res):
        if not node:
            return res
        res.append(node.val)
        self.helper(node.left, res)
        self.helper(node.right, res)
        return res

迭代:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        stack = [root]
        res = []
        while stack:
            cur = stack.pop()
            res.append(cur.val)
            if cur.right:
                stack.append(cur.right)
            if cur.left:
                stack.append(cur.left)
        return res

如果加在前面可以这样写:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        stack = [root]
        res = []
        while stack:
            cur = stack.pop(0)
            res.append(cur.val)
            if cur.right:
                stack = [cur.right] + stack
            if cur.left:
                stack = [cur.left] + stack
        return res

589. N叉树的前序遍历

递归:

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        return self.helper(root, [])

    def helper(self, node, res):
        if not node:
            return res
        res.append(node.val)
        for i in node.children:
            self.helper(i,res)
        return res

迭代:

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        if not root:
            return []
        stack = [root]
        res = []

        while stack:
            cur = stack.pop()
            res.append(cur.val)
            if cur.children:
                stack += cur.children[::-1]
        return res

中序遍历

94. 二叉树的中序遍历

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        return self.helper(root, [])
    
    def helper(self, node, res):
        if not node:
            return res
        if node.left:
            self.helper(node.left, res)
        res.append(node.val)
        if node.right:
            self.helper(node.right, res)
        return res

迭代:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        res = []
        stack = []
        cur = root
        while cur or stack:
            if cur:
                stack.append(cur)
                cur = cur.left
            else:
                cur = stack.pop()
                res.append(cur.val)
                cur = cur.right
        return res

后序遍历

145. 二叉树的后序遍历

递归:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        return self.helper(root, [])
    
    def helper(self, node, res):
        if not node:
            return res
        if node.left:
            self.helper(node.left, res)
        if node.right:
            self.helper(node.right, res)
        res.append(node.val)
        return res

迭代:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        stack = [root]
        stack2 = []
        while stack:
            node = stack.pop()
            stack2.append(node)
            if node.left:
                stack.append(node.left)
            if node.right:
                stack.append(node.right)
        return [i.val for i in stack2[::-1] ]

或:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        
        tmp = [root]
        res = []
        while tmp:
            cur = tmp.pop()
            res.append(cur.val)
            if cur.left:
                tmp.append(cur.left)
            if cur.right:
                tmp.append(cur.right)
        return res[::-1]

590. N叉树的后序遍历

递归:

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def postorder(self, root: 'Node') -> List[int]:
        return self.helper(root, [])
    
    def helper(self, node, res):
        if not node:
            return res
        if node.children:
            for i in node.children:
                self.helper(i, res)
        res.append(node.val)
        return res

层序遍历

102. 二叉树的层序遍历

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        res = []
        cur = [root]
        while cur:
            tmp = []
            nex = []
            for i in cur:
                tmp.append(i.val)
                if i.left:
                    nex.append(i.left)
                if i.right:
                    nex.append(i.right)
            cur = nex
            res.append(tmp)
        return res

429. N叉树的层序遍历

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        if not root:
            return []
        res = []
        cur = [root]
        while cur:
            tmp = []
            nex = []
            for i in cur:
                tmp.append(i.val)
                if i.children:
                    nex += i.children
            cur = nex
            res.append(tmp)
        return res
上一篇下一篇

猜你喜欢

热点阅读