二叉树相关

本文介绍了如何使用深度优先搜索(DFS)和广度优先搜索(BFS)算法计算二叉树的最大深度、最小深度和最大宽度。同时,还讨论了前序遍历和后序遍历的方法。
摘要由CSDN通过智能技术生成

二叉树深度和宽度

二叉树最大,最小深度

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

class Solution():

    def maxDepth(self, root):
        """
        root: TreeNode
        returntype: int
        """
        if root is None:
            return 0
        else:
            left_height = self.maxDepth(root.left)
            right_height = self.maxDepth(root.right)
            return max(left_height, right_height) +1

    # 以上:深度优先搜索,时间复杂度O(N)-每个节点递归中只被遍历一次,空间复杂度O(logN)-递归需要栈空间O(height)。

    def minDepth(self, root):
        if not root:
            return 0
        if not root.left and not root.right:
            return 1
        min_depth = 10**9
        if root.left:
            min_depth = min(self.minDepth(root.left), min_depth)
        if root.right:
            min_depth = min(self.minDepth(root.right), min_depth)
        return min_depth + 1

    # 以上:深度优先搜索,时间复杂度O(N)-每个节点递归中只被遍历一次,空间复杂度O(logN)-递归需要栈空间O(height)。

    def minDepth(self, root: TreeNode) -> int:
        if not root:
            return 0

        que = collections.deque([(root, 1)])
        while que:
            node, depth = que.popleft()
            if not node.left and not node.right:
                return depth
            if node.left:
                que.append((node.left, depth + 1))
            if node.right:
                que.append((node.right, depth + 1))
        
        return 0

    # 以上:广度优先搜索,时间复杂度O(N)-每个节点递归中只被遍历一次,空间复杂度O(N)-空间复杂度主要取决于队列的开销,队列中的元素个数不会超过树的节点数。

 二叉树最大宽度

二叉树最大宽度

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

class Solution():

    def maxWidth(self, root):


    # 以上:深度优先搜索,时间复杂度O(N)-每个节点递归中只被遍历一次,空间复杂度O(N)


    def maxWidth(self, root: TreeNode) -> int:
        res = 1
        arr = [[root, 1]]
        while arr:
            tmp = []
            for node, index in arr:
                if node.left:
                    tmp.append([node.left, index * 2])
                if node.right:
                    tmp.append([node.right, index * 2 + 1])
            res = max(res, arr[-1][1] - arr[0][1] + 1)
            arr = tmp
        return res

    # 以上:广度优先搜索,时间复杂度O(N)-每个节点递归中只被遍历一次,空间复杂度O(N)

前序后序遍历

图解 二叉树的四种遍历

# 前序遍历
class Solution:

    def preorderTraversal(self, root: TreeNode) -> List[int]:
        def preorder(root: TreeNode):
            if not root:
                return
            res.append(root.val)
            preorder(root.left)
            preorder(root.right)

        res = list()
        preorder(root)
        return res


    def postorderTraversal(self, root: TreeNode) -> List[int]:
        def postorder(root: TreeNode):
            if not root:
                return
            postorder(root.left)
            postorder(root.right)
            res.append(root.val)
        res = list()
        postorder(root)
        return res
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值