学习记录@代码随想录day16:二叉树part03

学习记录@代码随想录day16:二叉树part03

104.二叉树的最大深度

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

题目链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/submissions/

#递归,这题做过
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
       return self.get_depth(root)

    def get_depth(self, node):
        if not node:
            return 0
        #后序
        left_depth=self.get_depth(node.left)
        right_depth=self.get_depth(node.right)

        height=1+max(left_depth,right_depth)
        return height

559.n叉树的最大深度

给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。

题目链接:https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/
根节点的最大高度就是二叉树的深度。
求高度用后序遍历,深度用前序遍历。

class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root:
            return 0
        max_depth=1
        #后序遍历,求每个孩子节点的深度
        for child in root.children:
            max_depth=max(max_depth,self.maxDepth(child)+1)
        return max_depth

思想和二叉树一样。

111.二叉树的最小深度

给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

题目链接:https://leetcode.cn/problems/minimum-depth-of-binary-tree/

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        min_depth=1       
        left_depth=self.minDepth(root.left)
        right_depth=self.minDepth(root.right)
        min_depth=min(left_depth,right_depth)+1
      
        return min_depth

以上是自己写的代码,通过部分用例,但是没有成功,查看了下原因发现是当节点没有左节点或者右节点时,就会出错,因为没有左右孩子,最小深度就是0+1,而不管另一个孩子的情况。

class Solution:
    def getDepth(self, node):
        if node is None:
            return 0
        leftDepth = self.getDepth(node.left)  # 左
        rightDepth = self.getDepth(node.right)  # 右
        
        # 当一个左子树为空,右不为空,这时并不是最低点
        if node.left is None and node.right is not None:
            return 1 + rightDepth
        
        # 当一个右子树为空,左不为空,这时并不是最低点
        if node.left is not None and node.right is None:
            return 1 + leftDepth
        
        result = 1 + min(leftDepth, rightDepth)
        return result

    def minDepth(self, root):
        return self.getDepth(root)

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

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        count=1
        queue=collections.deque([root])
        while queue:
           
            cur =queue.popleft()
            if cur.left:
                count+=1
                queue.append(cur.left)
            if cur.right:
                count+=1
                queue.append(cur.right)
           
        return count

自己写了迭代法
写递归法的出错了

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        count=1
        left_num=self.countNodes(root.left)
        right_num=self.countNodes(root.right)
        if root.left:
            count+=1
        if root.right:
            count+=1
        return count

返回值不应该是和,应该是节点才对,所以这里应该新建方法。来递归每个节点
返回值为节点,终止条件为叶子节点,递归逻辑为左节点个数加右节点个数加1。

class Solution:
    def countNodes(self, root: TreeNode) -> int:
        return self.getNodesNum(root)
        
    def getNodesNum(self, cur):
        if not cur:
            return 0
        leftNum = self.getNodesNum(cur.left) #左
        rightNum = self.getNodesNum(cur.right) #右
        treeNum = leftNum + rightNum + 1 #中
        return treeNum

还是不太理解递归呀,再好好看看吧。

总结

今天是昨日任务的补卡,o(╥﹏╥)o,尽力跟上进度呀。加油加油!
参考链接:https://docs.qq.com/doc/DUHBQRm1aSWR4T2NK

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值