signed

QiShunwang

“诚信为本、客户至上”

数据结构总结

2021/6/3 17:14:38   来源:

数据结构

  • 二叉树
    • 深度优先遍历(递归、非递归)
      • 前序遍历
      • 中序遍历
      • 后序遍历
    • 广度优先遍历(层序遍历)
    • 最短路
    • 前k条最短路
    • 深度优先搜索(DFS)
    • 广度优先搜索(BFS)

二叉树

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

深度优先遍历(递归、非递归)

前序遍历

  1. 递归
class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        return [root.val] + self.inorderTraversal(root.left) + self.inorderTraversal(root.right)
  1. 非递归
class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        stack = []
        res = []
        while root or stack:
            if root:
                res.append(root.val)
                stack.append(root)
                root = root.left
            else:
                root = stack.pop()
                root = root.right
        return res

中序遍历

  1. 递归
class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
  1. 非递归
class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        s = []
        res = []
        while root or s:
            if root: 
                s.append(root)
                root = root.left
            else:
                root = s.pop()
                res.append(root.val)
                root = root.right
        return res

后序遍历

  1. 递归
class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        return self.inorderTraversal(root.left) + self.inorderTraversal(root.right) + [root.val]
  1. 非递归
  • 方法一:记录上一个遍历时经过的节点,若是父节点继续向下遍历;若是左儿子且右儿子存在,则遍历右儿子;否则,输出当前节点的值。
class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        stack = [root]
        res = []
        pre = None
        while stack:
            current = stack[-1]
            if not pre or pre.left == current or pre.right == current:
                if current.left:
                    stack.append(current.left)
                elif current.right:
                    stack.append(current.right)
            elif pre == current.left and current.right:
                stack.append(current.right)
            else:
                res.append(current.val)
                stack.pop()
            pre = current
        return res
  • 方法二:类似先序遍历,改为根右左,反转。
class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        stack = []
        res = []
        while stack or root:
            if root:
                res.append(root.val)
                stack.append(root)
                root = root.right
            else:
                root = stack.pop()
                root = root.left
        res.reverse()
        return res

广度优先遍历(层序遍历)

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

最短路

前k条最短路

深度优先搜索(DFS)


广度优先搜索(BFS)