数据结构算法——树搜索

目录

  • 概述
  • 例题
    • 2.1 实现一个对二叉树广度优先搜索的函数
    • 2.2 实现一个对二叉树深度优先搜索的函数
    • 2.3 二叉树的层次遍历
    • 2.4 二叉树的最小深度
    • 2.5 括号生成

概述

该博客结合leetcode原题介绍了可以使用树结构搜索算法的常见题目。

例题

2.1 实现一个对二叉树广度优先搜索的函数

# encoding = utf-8
"""
本脚本用于自己设计一个广度优先算法的函数
"""

# 定义tree的数据类型
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

root = TreeNode(0)
root.left = TreeNode(2)
root.right = TreeNode(4)
root.left.left = TreeNode(5)

def generate_related_nodes(visited, node):
    next_node_list = []
    if node.left not in visited and node.left:
        next_node_list.append(node.left)
    if node.right not in visited and node.right:
        next_node_list.append(node.right)
    return next_node_list

# 广度优先搜索主函数
def BFS(tree, start):
    queue = []
    queue.append(start)
    visited = {}
    visited[start] = 0

    while queue:
        node = queue.pop(0)  # 从数组头部依次拿出每个数据,访问每个节点
        visited[node] = 0  # 记录访问过的节点
        print(node.val)  # 对节点进行一些操作
        queue.extend(generate_related_nodes(visited, node))  # 将下一层节点插在数组尾部


if __name__=="__main__":
    BFS(root, root)

2.2 实现一个对二叉树深度优先搜索的函数

# encoding = utf-8

"""
本脚本用于自己设计一个广度优先算法的函数
"""

# 定义tree的数据类型
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

root = TreeNode(0)
root.left = TreeNode(2)
root.right = TreeNode(4)
root.left.left = TreeNode(5)

visited = {}
def DFS(root):
    visited[root] = 1
    print(root.val)

    for node in [root.left, root.right]:
        if node and node not in visited:
            DFS(node)


if __name__=="__main__":
    DFS(root)

2.3 二叉树的层次遍历

#Leetcode 102 二叉树的层次遍历
(1)BFS
最直观的办法。

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        queue = [root]
        res = []
        while queue:
            level_size = len(queue)
            cur_level = []
            for _ in range(level_size):
                node = queue.pop(0)
                cur_level.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            res.append(cur_level)
        return res
                    

*时间复杂度O(N)
*空间复杂度O(1)
(2)DFS
记录level,重新复盘


*时间复杂度O(N)
*空间复杂度O(N)

2.4 二叉树的最小深度

#111. 二叉树的最小深度
(1)BFS

# encoding = utf-8

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

root = TreeNode(0)
root.left = TreeNode(2)
root.right = TreeNode(4)
root.left.left = TreeNode(5)

def generate_related_nodes(visited, node):
    next_node_list = []
    for next_node in [node[0].left, node[0].right]:
        if next_node and next_node not in visited:
            next_node_list.append([next_node, node[1]+1])
    return next_node_list

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root: return 0
        visited = {}
        level = 1
        queue = []
        visited[root] = 1
        queue.append([root, level])
        while queue:
            node = queue.pop(0)
            if not node[0].left and not node[0].right:
                return node[1]
            queue.extend(generate_related_nodes(visited, node))

if __name__=="__main__":
    s = Solution()
    print(s.minDepth(root))

(2)DFS
要记录深度level,更新max和min

2.5 括号生成

#Leetcode 22 括号生成
(1)递归

*时间复杂度O(2^N)

(2)递归基础上剪枝优化
*时间复杂度O(2^N)

# coding = utf-8

class Solution(object):
    def __init__(self):
        self.res = []

    def fun(self, left, right, n, s):
        if left==0 and right==0:
            self.res.append(s)
            return
        if left>0:
            self.fun(left - 1, right, n, s + '(')
        if right>0 and right>left:
            self.fun(left, right - 1, n, s + ')')

    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        s = ''
        left = n
        right = n
        self.fun(left, right, n, s)
        return self.res


if __name__=="__main__":
    s = Solution()
    print(s.generateParenthesis(3))

(3)栈

你可能感兴趣的:(数据结构算法)