其他分享
首页 > 其他分享> > 【基础】广度优先搜索 & 深度优先搜索 (待补)

【基础】广度优先搜索 & 深度优先搜索 (待补)

作者:互联网

DFS

二叉树的遍历

class Solution(object):
    def maxDepth_dfs(self, root):
        if not root:
            return 0
        queue = [root]
        height = 0
        while queue:
            currentSize = len(queue)
            node = queue.pop(0)
            print(node)
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
        return height

作者:zhi-xiao-3
链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/solution/by-zhi-xiao-3-lk8r/

BFS

二叉树的遍历

class Solution(object):
    def maxDepth_bfs(self, root):
        if not root:
            return 0
        queue = [root]
        height = 0
        while queue:
            currentSize = len(queue)
            node = queue.pop()
            print(node)
            if node.right:
                queue.append(node.right)
            if node.left:
                queue.append(node.left)
        return height

作者:zhi-xiao-3
链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/solution/by-zhi-xiao-3-lk8r/

标签:node,优先,right,待补,xiao,height,queue,搜索,root
来源: https://www.cnblogs.com/Jojo-L/p/16469042.html