代码随想录Day16 | 二叉树深度 and 节点个数!

继续继续二叉树啦!
先补一下前一天的一道拓展题目:

572 另一棵树的子树

在这里插入图片描述
这道题要注意的是,在递归写法下,如果判断不正确也不能直接返回false,应该在不正确的情况下继续遍历左右节点,这样才能保证AC。

class Solution:
    def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
        if not root and not subRoot: return True
        stack = collections.deque([root])
        while stack:
            node = stack.popleft()
            if node.val == subRoot.val: 
                result = self.compare(node, subRoot)
                if result:
                    return True
            if node.left: stack.append(node.left)
            if node.right: stack.append(node.right)
        return False
    def compare(self, left, right):
        if not left and not right: return True
        elif not left or not right or left.val != right.val: return False
        l = self.compare(left.left, right.left)
        r = self.compare(left.right, right.right)
        return l and r

104. 二叉树的最大深度

首先要理解什么是深度,深度其实就是最长的“枝叶”。那么最方便的方式是用递归来做。重点的重点是理解如何存储深度,也就是1+max(left_height, right_heiht)。

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        return self.get_depth(root)
    def get_depth(self, root):
        if not root:
            return 0
        left_height = self.get_depth(root.left)
        right_height = self.get_depth(root.right)
        height = 1 + max(left_height, right_height)
        return height

同时,深度也是这颗二叉树的“层数”,因此可以用层序遍历所对应的迭代法来做。

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

599. n叉树的最大深度

这个和二叉树的重点区别在于有多个子节点,并且数量不确定,因此每次都要判断一次max。

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

层序遍历也是一样的思路,这里就不累述啦。

111. 二叉树的最小深度

关于这道题需要注意的是如何去度量“最小”。如果用递归法,关键就是考虑不同的特殊情况;如果用层序法,就要知道终止条件是什么。一定要考虑left/right不存在的情况,不能只用min来进行操作。
这里需要注意的是一旦没有深度了说明到底了,是最小的了。

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        return self.get_depth(root)
    def get_depth(self, root):
        if root is None: return 0
        left = self.get_depth(root.left)
        right = self.get_depth(root.right)
        if not left and right: return 1+right
        if left and not right: return 1+left
        return 1 + min(left, right)

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

在这里插入图片描述
首先我们完全可以按照普通二叉树的方式来求,也就是用层序遍历,然后每碰到一个节点就记录即可。但考虑其是完全二叉树,也就是只有最后一层没有满,并且所有节点集中在左边,就可以利用这个特性来写。
具体而言,是要找到“满二叉树”。如果左边和右边同时到底则是满二叉树,这时候直接用2**层数-1返回节点数即可。否则就继续往下遍历,直到左右都是满二叉树为止。

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if not root: return 0
        left = root.left
        right = root.right
        count = 1
        while left and right:
            count += 1
            left = left.left
            right = right.right
        if not left and not right:
            return 2**count - 1
        return 1 + self.countNodes(root.left) + self.countNodes(root.right)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值