代码随想录算法训练营第16天| Binary Tree|● 104.二叉树的最大深度 559.n叉树的最大深度● 111.二叉树的最小深度● 222.完全二叉树的节点个数

104. Maximum Depth of Binary Tree

  • 递归法,后序遍历
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:  

        def recur(node):
            if not node:
                return 0
            h_left = recur(node.left)
            h_right = recur(node.right)

            return max(h_left,h_right)+1

        return recur(root)

559. Maximum Depth of N-ary Tree

Given a n-ary tree, find its maximum depth.

Sol 1:

class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root:
            return 0

        max_h=1
        for child in root.children:
            max_h = max(self.maxDepth(child)+1,max_h)

        return max_h 

Sol 2:

#bfs
class Solution1:
    def maxDepth(self, root: 'Node') -> int:
        if not root:
            return 0
        queue = deque([root])
        res = 0

        while queue:
            res+=1
            size = len(queue)
            for _ in range(size):
                node = queue.popleft()
                for x in node.children:
                    if x:
                        queue.append(x)
        return res

111. Minimum Depth of Binary Tree

Sol 1: 递归

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:

        def min_recur(root):
            if not root:
                return 0
            h_left = min_recur(root.left)
            h_right = min_recur(root.right)

            if not root.left and not root.right:
                h = 1
            elif not root.left and root.right:
                h = h_right+1
            elif not root.right and root.left:
                h = h_left+1
            else:
                h = min(h_left, h_right)+1
            return h
        return min_recur(root)

Sol 2: BFS

class Solution1:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        queue = deque([root])
        depth = 1
        while queue:
            size = len(queue)
            for _ in range(size):
                node = queue.popleft()
                if not node.left and not node.right:
                    return depth
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            depth+=1
        return depth

222. Count Complete Tree Nodes

Given the root of a complete binary tree, return the number of the nodes in the tree.

According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

Design an algorithm that runs in less than O(n) time complexity.

Example 1:

img
Input: root = [1,2,3,4,5,6]
Output: 6

Sol 1: Complete tree formula

# Complete Tree
class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:

        def count_recur(node)-> int:
            if not node:
                return 0
            left, right = node.left, node.right
            d_l, d_r = 0, 0
            while left:
                d_l+=1
                left = left.left
            while right:
                d_r+=1
                right = right.right
            
            if d_l == d_r:
                return (2<<d_l) - 1
            
            left_num = count_recur(node.left)
            right_num = count_recur(node.right)

            return left_num + right_num + 1

        return count_recur(root)

Sol 2: dfs recur

# Recur
class Solution2:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        def count_recur(node) -> int:
            if not node:
                return 0
            left = count_recur(node.left)
            right = count_recur(node.right)

            return left+right+1

        return count_recur(root)

Sol3: BFS

class Solution1:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        queue = deque([root])
        res = 0
        while queue:
            size = len(queue)
            level = 0
            for _ in range(size):
                node = queue.popleft()
                level+=1
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            res+=level
        return res
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值