代码随想录 二叉树Ⅲ

104. 二叉树的最大深度

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

Python: 递归法,求根结点的高度(离叶子结点的最大距离)

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

        return height

Python:层序遍历

# 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
        ans = 0
        que = collections.deque([root])

        while que:
            for i in range(len(que)):
                head = que.popleft()
                if head.left: que.append(head.left)
                if head.right: que.append(head.right)

            ans += 1
        
        return ans

559. N 叉树的最大深度

Python : 使用栈

"""
# 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:
        if not root:
            return 0
        
        max_depth = 0
        
        stack = [(root, 1)]
        
        while stack:
            node, depth = stack.pop()
            max_depth = max(max_depth, depth)
            for child in node.children:
                stack.append((child, depth + 1))
        
        return max_depth

111. 二叉树的最小深度

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明:叶子节点是指没有子节点的节点。

Python: 层序遍历

# 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

        ans = 1
        que = collections.deque([root])

        while que:
            for i in range(len(que)):
                head = que.popleft()
                if not head.left and not head.right:
                    return ans

                if head.left: que.append(head.left)
                if head.right: que.append(head.right)

            ans += 1

Python:递归,后序遍历

# 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

        return self.postrecur(root)

    def postrecur(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
            
        left_depth = self.postrecur(root.left)
        right_depth = self.postrecur(root.right)
        if root.left and not root.right:
            return 1 + left_depth

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

        
        return 1 + min(left_depth, right_depth)

Python:前序遍历

class Solution:
    def __init__(self):
        self.ans = float('inf')

    def getDepth(self, node, depth):
        if node is None:
            return
        if node.left is None and node.right is None:
            self.ans = min(self.result, depth)
        if node.left:
            self.getDepth(node.left, depth + 1)
        if node.right:
            self.getDepth(node.right, depth + 1)

    def minDepth(self, root):
        if root is None:
            return 0
        self.getDepth(root, 1)
        return self.ans

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

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

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

Python:不考虑完全二叉树性质

# 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 == None:
            return 0

        ans = 0 
        queue = collections.deque([root])

        while queue:
            for i in range(len(queue)):
                head = queue.popleft()
                ans += 1
                if head.left: queue.append(head.left)
                if head.right: queue.append(head.right)

        return ans

Python:利用完全二叉树性质

class Solution: 
    def countNodes(self, root: TreeNode) -> int:
        if not root: return 0
        ans = 1
        left = root.left
        right = root.right
        while left and right:
            ans += 1
            left = left.left
            right = right.right
        if not left and not right: # 如果同时到底说明是满二叉树,反之则不是
            return 2**ans-1
        return 1+self.countNodes(root.left)+self.countNodes(root.right) 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值