代码随想录 Day16

leetcode 104. 二叉树最大深度 

这道题目非常考察对于二叉树的理解。

首先明确:对于求高度,是从下到上的,所以是后序遍历。先获取左右两个的高度,然后在最大基础上加1就得到了现在的高度。对于求深度 ,要采用先序遍历 ,往下传递结果。

后序遍历:

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

 先序遍历:

输入:当前节点、当前节点深度;输出:当前节点下的最大深度

两种思路,第一种,维护一个全域的max_depth;第二种,递归传递当前最大深度。第二种更好一些。

class Solution:
    def findMaxDepth(self, node, current_depth):
        """
        递归计算当前节点的深度,并返回其下最大深度。
        
        :param node: 当前处理的节点
        :param current_depth: 当前节点的深度
        """
        if not node:
            return current_depth - 1

        else:
            left_max_depth = self.findMaxDepth(node.left, current_depth+1)
            right_max_depth = self.findMaxDepth(node.right, current_depth+1)

        return max(left_max_depth, right_max_depth)

        
    def maxDepth(self, root):
        """
        计算二叉树的最大深度。
        
        :param root: 二叉树的根节点
        :return: 二叉树的最大深度
        """
        return self.findMaxDepth(root, 1)

附:对于n叉树:

class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root:
            return 0
        else:
            depth = 0
            for each in root.children:
                depth = max(depth, self.maxDepth(each))
            return depth+1

leetcode 111. 二叉树的最小深度

# 我的做法,借鉴104,只是对于处理左/右为空节点做了特殊处理:

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        else:
            left_depth = self.minDepth(root.left)
            right_depth = self.minDepth(root.right)
            if left_depth != 0 and right_depth != 0:
                depth = min(left_depth, right_depth) + 1
            elif left_depth == 0:
                depth = right_depth + 1
            elif right_depth == 0:
                depth = left_depth + 1

        return depth

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

# 思路1:当作普通的二叉树来解决

则使用后续遍历方式,获取左、右子树个数,完全遍历并记录。

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        else:
            left_nodes = self.countNodes(root.left)
            right_nodes = self.countNodes(root.right)
            return left_nodes + right_nodes + 1

 时间复杂度为:O(n)

# 思路2:使用完全二叉树和满二叉树的特性

特性:

1. 完全二叉树的子树可以更容易判断出是不是满二叉树

2. 满二叉树节点计算:2^n - 1

做法:

1. 判断子树是不是满二叉树,如果是,那就可以直接计算得到所有左右子节点数。

2. 如果有一个不是,那就拆开成左右两个

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0

        else:
            left_depth = 0
            right_depth = 0
            left_node = root
            right_node = root
            while left_node:
                left_depth += 1
                left_node = left_node.left
            while right_node:
                right_depth += 1
                right_node = right_node.right
            if left_depth == right_depth:
                return 2**left_depth - 1
            else:
                return self.countNodes(root.left) + self.countNodes(root.right) + 1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值