leetcode 打卡 day16

104.二叉树的最大深度 559.n叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

104.二叉树的最大深度

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 maxDepth(self, root: Optional[TreeNode]) -> int:
        def getHeight(node):
            if not node:
                return 0
            left_height = getHeight(node.left)
            right_height = getHeight(node.right)
            height = max(left_height, right_height) + 1
            return height
        return getHeight(root)

精简版:

# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

559.n叉树的最大深度

方法一:递归法
1、children数据结构是list,列表。
2、 height = max(height, getHeight(node.children[i])) ## 找到这层孩子结点的最大高度。
3、height + 1 # 找到这层孩子结点高度的上一层高度。

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""
class Solution:
    def maxDepth(self, root: 'Node') -> int:
        def getHeight(node):
            if not node:
                return 0
            height = 0
            for i in range(len(node.children)):
                height =  max(height, getHeight(node.children[i]))
            return height + 1
        return getHeight(root)

方法二:迭代法

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""
from collections import deque
class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root: return 0 
        max_level = 0
        queue = collections.deque()
        queue.append(root)
        while queue:
            for _ in range(len(queue)):
                node = queue.popleft()
                for child in node.children:
                    queue.append(child)
            max_level += 1
        return max_level

111.二叉树的最小深度

1、使用左右中后续遍历进行遍历。
2、注意:考虑到左子树为空,右子树不为空 和 右子树为空,左子树不为空的情况下。返回左子树的最小值+1, 和返回右子树的最小值+1.

# 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 minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        def getHeight(node):
            if not node:
                return 0
            leftHeight = getHeight(node.left)
            rightHeight = getHeight(node.right)
            if not node.left and node.right:
                return 1 + rightHeight
            if not node.right and node.left:
                return 1 + leftHeight
            return 1 + min(leftHeight, rightHeight)
        return getHeight(root)

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

1、使用后序遍历

# 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:
        def getHeight(node):
            if not node:
                return 0
            leftNum = getHeight(node.left)
            rightNum = getHeight(node.right)
            res = 1 + leftNum + rightNum
            return res
        return getHeight(root)

完全二叉树解法:
后序遍历

class Solution:
    def countNodes(self, root: TreeNode) -> int:
        if not root:
            return 0
        left = root.left
        right = root.right
        leftDepth = 0 #这里初始为0是有目的的,为了下面求指数方便
        rightDepth = 0
        while left: #求左子树深度
            left = left.left
            leftDepth += 1
        while right: #求右子树深度
            right = right.right
            rightDepth += 1
        if leftDepth == rightDepth:
            return (2 << leftDepth) - 1 #注意(2<<1) 相当于2^2,所以leftDepth初始为0
        return self.countNodes(root.left) + self.countNodes(root.right) + 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值