代码随想录算法训练营第十三天| 104.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

文档讲解:代码随想录

视频讲解:代码随想录B站账号

状态:看了视频题解和文章解析后做出来了

104.二叉树的最大深度

1. 递归

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0

        result = self.depth(root)

        return result

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

        left_height = self.depth(root.left)
        right_height = self.depth(root.right)

        return 1 + max(left_height, right_height)

思路:

  • 前序遍历:得到的是深度,因为中节点优先处理,0->1->2每次遍历的是当前层的深度
  • 后序遍历:得到的是高度,因为子节点的高度返回给母节点,所以是到根节点是高度

递归第一步:确定参数和返回值,因为我们需要深度/高度所以肯定返回整数。

递归第二步:确定终止条件,当节点为空,返回0,也就是空节点的深度

递归第三步:确定每一层的处理逻辑,递归左节点和右节点,最后返回max+1。思考一下如果是空节点,那就会返回给母节点1.

2. 层序遍历

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0

        que = deque([root])
        result = 0

        while que:
            for i in range(len(que)):
                cur = que.popleft()
                if cur.left:
                    que.append(cur.left)
                if cur.right:
                    que.append(cur.right)
            result += 1

        return result

思路:依然觉得层序遍历是最直接的方式,直接遍历每层,遍历完一层result += 1就完事了。

111.二叉树的最小深度

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0

        result = 0
        result = self.depth(root)

        return result


    def depth(self, root):
        if not root:
            return 0

        left_height = self.depth(root.left)
        right_height = self.depth(root.right)

        if root.left and not root.right:
            return 1 + left_height
        elif root.right and not root.left:
            return 1 + right_height

        return 1 + min(left_height, right_height)

细节:这道题和最大深度相似,使用后序遍历。但有一个小坑,就是在返回值的时候,首先要判断左子树和右子树是否为空,如果为空就不能返回它们的深度,因为这样取min就一定是0了。题目要求的是根节点到叶节点的最小深度。

222.完全二叉树的节点个数

递归

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        return self.count(root)

    def count(self, root):
        if not root:
            return 0

        left_count = self.count(root.left)
        right_count = self.count(root.right)

        return left_count + right_count + 1

层序遍历

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0

        que = deque([root])
        count = 1
        while que:
            for i in range(len(que)):
                cur = que.popleft()
                if cur.left:
                    que.append(cur.left)
                if cur.right:
                    que.append(cur.right)
            count += len(que)
            
        return count

思路:让我们喊出那句话:层序遍历永远的神。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值