104. 二叉树的最大深度
递归实现:
终止条件:空节点,对树的深度贡献为0。
递归过程:左子树的深度和右子树的深度较大的加上该节点的1就是以该节点为根节点的树的最大深度。
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))
迭代实现:
层序遍历:
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
que = collections.deque([root])
res = 0
while que:
level_size = len(que)
res += 1
for _ in range(level_size):
node = que.popleft()
if node.left: que.append(node.left)
if node.right: que.append(node.right)
return res
559. N 叉树的最大深度
递归实现:
class Solution:
def maxDepth(self, root: 'Node') -> int:
if not root:
return 0
return 1 + max([self.maxDepth(child) for child in root.children],default = 0)
迭代实现:
class Solution:
def maxDepth(self, root: 'Node') -> int:
if not root:
return 0
res = 0
que = collections.deque([root])
while que:
level_size = len(que)
res += 1
for _ in range(level_size):
node = que.popleft()
for child in node.children:
que.append(child)
return res
111. 二叉树的最小深度
递归实现:
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
elif root.left and not root.right:
return 1+self.minDepth(root.left)
elif root.right and not root.left:
return 1+self.minDepth(root.right)
return 1+min(self.minDepth(root.left),self.minDepth(root.right))
要注意的是最小深度指的是从根节点到一个叶子节点的深度,如果一个节点本身只有一个儿子,那就需要从另一个儿子那里找子树的最小深度并+1,而不是简单的return 1+0。
迭代实现:
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
res = 0
que = collections.deque([root])
while que:
level_size = len(que)
res += 1
for _ in range(level_size):
node = que.popleft()
if not node.left and not node.right:
return res
else:
if node.left: que.append(node.left)
if node.right: que.append(node.right)
222. 完全二叉树的节点个数
不考虑“完全二叉树”,当成普通的二叉树:
class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root: return 0
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
考虑完全二叉树的特性:
方法一:
我的逻辑跟代码随想录的逻辑不太一样:
终止条件:空节点,返回0
递归过程:给定一个节点,考察他的左右子树的高度,由于是完全二叉树,左子树的高度要么和右子树一样,要么比他大。如果一样,左子树就是满二叉树,直接求出节点数量,再递归地求右子树的节点数量。如果左子树比右子树高,说明最终叶子节点停在左子树下,那么右子树就是满二叉树,直接求出节点数量,再递归地求左子树地节点数量。
class Solution:
def getDepth(self, node):
depth = 0
while node:
depth += 1
node = node.left
return depth
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
leftDepth = self.getDepth(root.left)
rightDepth = self.getDepth(root.right)
if leftDepth == rightDepth:
return (1 << leftDepth) + self.countNodes(root.right)
else:
return (1 << rightDepth) + self.countNodes(root.left)
方法二:
先考虑当前节点下的树是否是满二叉树,如果是,则直接求节点数量。如果不是,递归地寻找左子树和右子树中的满二叉树并用于求左子树和右子树的节点数量和。
修改后的代码:
class Solution:
def getDepth(self, node, isLeft):
depth = 0
while node:
depth += 1
if isLeft:
node = node.left
else:
node = node.right
return depth
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
leftDepth = self.getDepth(root, True)
rightDepth = self.getDepth(root, False)
if leftDepth == rightDepth:
return (1 << leftDepth) - 1
else:
return self.countNodes(root.left) + self.countNodes(root.right) + 1
今日总结:
递归递归递归递归递归...
最后一题其实不是很能理解代码随想录的方法,我觉得想不出来先判断返回满二叉树的值,然后递归地找满二叉树。我直接会想到比一下左右子树高度,因为左右子树必有一个是满二叉树,因此直接计算一个并且递归地计算另一个。可能有点先入为主的影响,感觉代码随想录的方法特别难以理解,但是仔细想了下自己的方法也是O(log N * log N)的,两种方法递归本质应该是一样的。可能在最坏的情况下要更优一些?
文章探讨了如何使用递归和迭代方法计算二叉树和N叉树的最大深度、最小深度以及完全二叉树的节点个数。作者分析了递归和迭代实现的区别,并提供了两种不同的解决方案。

被折叠的 条评论
为什么被折叠?



