代码随想录No16 |LeetCode 104.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数

二叉树深度、节点个数

今天开始开始二叉树第三天的题了!

104.二叉树的最大深度

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

解法一:递归法_后序遍历 左右中

# 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 Depth(root):
            if not root: return 0

            left_depth = Depth(root.left)
            right_depth = Depth(root.right)
            max_depth = 1 + max(left_depth,right_depth)
            return max_depth

        return Depth(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:
        # 层序遍历
        from collections import deque
        if not root: return 0

        depth = 0
        que = deque([root])
        while que:
            size = len(que)
            depth +=1
            for _ in range(size):
                cur = que.popleft()
                if cur.left:que.append(cur.left)
                if cur.right:que.append(cur.right)

        return depth

111.二叉树的最小深度

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

本题要注意最小深度的定义,是到最近叶子节点的深度,如果某一支没有深度,另一只有深度,那么最小深度不是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:
        def getdepth(node):
            if not node:return 0
            left_depth = getdepth(node.left)
            right_depth = getdepth(node.right)

            if node.left and not node.right:
                return 1+ left_depth

            if not node.left and node.right:
                return 1 + right_depth

            res = 1 + min(left_depth,right_depth)
            return res

        return getdepth(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 minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:return 0
        from collections import deque

        que = deque([root])
        min_depth = 0

        while que:
            size = len(que)
            min_depth += 1
            for _ in range(size):
                node = que.popleft()
                if node.left:que.append(node.left)
                if node.right:que.append(node.right)
                if not node.left and not node.right:
                    return min_depth

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

给你一棵完全二叉树的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2 h 2^h 2h个节点。

解法一:递归_后序遍历 左右中

# 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 count(node):
            if not node: return 0
            left_num = count(node.left)
            right_num = count(node.right)
            treenode_num = left_num + right_num+1

            return treenode_num

        return count(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 countNodes(self, root: Optional[TreeNode]) -> int:
        if not root : return 0

        from collections import deque
        que = deque([root])
        res = 0
        while que:
            size = len(que)
            for _ in range(size):
                res += 1
                node = que.popleft()
                if node.left: que.append(node.left)
                if node.right: que.append(node.right)
        return res

以上其实是普通二叉树的做法,没有利用完全二叉树的特性。

完全二叉树:

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 
        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、付费专栏及课程。

余额充值