代码随想录算法训练营第16天|二叉树part03|104. 二叉树的最大深度、111. 二叉树的最小深度、 222.完全二叉树的节点个数(优先掌握递归)
104. 二叉树的最大深度
这道题层次遍历之后,手撕10道题中已经包含,就是利用层次遍历来做
代码
python
层次遍历属于迭代法
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
queue = collections.deque()
queue.append(root)
depth = 0
while queue:
depth += 1
for _ in range(len(queue)): # 点睛之笔!!!只遍历每层结点的长度
cur = queue.popleft()
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return depth
递归法
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
而根节点的高度就是二叉树的最大深度
本题:二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
代码:
python
后序遍历递归
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
leftDepth = self.maxDepth(root.left)
rightDepth = self.maxDepth(root.right)
depth = 1 + max(leftDepth, rightDepth)
return depth
前序遍历递归
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
max_Depth = 0
def maxDepth(self, root):
if not root:
return 0
self.compute(root, 1)
return self.max_Depth
def compute(self, root, depth):
if root == None:
depth -= 1
return
self.max_Depth = depth if depth > self.max_Depth else self.max_Depth
self.compute(root.left, depth+1)
self.compute(root.right, depth+1) # 修改此处的递归调用
111. 二叉树的最小深度
自己做
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
这道题层次遍历之后,手撕10道题中已经包含,就是利用层次遍历来做
代码
python
层次遍历属于迭代法
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
depth = 0
queue = collections.deque([root])
while queue:
depth += 1
for _ in range(len(queue)):
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)
return depth
递归
叶子结点:既没有左孩子也没有右孩子
前序递归遍历
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
depth = 1
return self.computeMinDepth(root, depth)
def computeMinDepth(self, root, depth): # 1
if root.left == None and root.right == None: # 2
return depth
if root.left and root.right: # 3
leftMinDepth = self.computeMinDepth(root.left, depth+1)
rightMinDepth = self.computeMinDepth(root.right, depth+1)
return leftMinDepth if leftMinDepth < rightMinDepth else rightMinDepth
elif root.left and not root.right:
return self.computeMinDepth(root.left, depth+1)
elif not root.left and root.right:
return self.computeMinDepth(root.right, depth+1)
代码随想录
本题依然是前序遍历和后序遍历都可以,前序求的是深度,后序求的是高度。
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)
代码:
python
class Solution:
def getDepth(self, node):
if node is None:
return 0
leftDepth = self.getDepth(node.left) # 左
rightDepth = self.getDepth(node.right) # 右
# 当一个左子树为空,右不为空,这时并不是最低点
if node.left is None and node.right is not None:
return 1 + rightDepth
# 当一个右子树为空,左不为空,这时并不是最低点
if node.left is not None and node.right is None:
return 1 + leftDepth
result = 1 + min(leftDepth, rightDepth)
return result
def minDepth(self, root):
return self.getDepth(root)
222. 完全二叉树的节点个数
自己做
层次遍历
python
from collections import deque
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def countNodes(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# 层次遍历
if not root: return 0
queue = collections.deque()
queue.append(root)
count = 0
while queue:
for _ in range(len(queue)):
cur = queue.popleft()
count += 1
if cur.left: queue.append(cur.left)
if cur.right: queue.append(cur.right)
return count
递归
class Solution:
def countNodes(self, root: TreeNode) -> int:
return self.getNodesNum(root)
def getNodesNum(self, cur):
if not cur:
return 0
leftNum = self.getNodesNum(cur.left) #左
rightNum = self.getNodesNum(cur.right) #右
treeNum = leftNum + rightNum + 1 #中
return treeNum
代码随想录
思想:
完全二叉树只有两种情况
-
情况一:就是满二叉树:可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1
-
情况二:最后一层叶子节点没有满:分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。
这里关键在于如何去判断一个左子树或者右子树是不是满二叉树呢?
在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。
代码:
python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def countNodes(self, root): # 1
"""
:type root: TreeNode
:rtype: int
"""
if not root: # 2
return 0
# 3
leftDepth = 0
rightDepth = 0
if root.left:
# 判断左子树是否为满二叉树
cur = root.left
while cur != None:
cur = cur.left
leftDepth += 1
if root.right:
# 判断右子树是否为满二叉树
cur = root.right
while cur != None:
cur = cur.right
rightDepth += 1
if leftDepth == rightDepth:
return 1 + 2**leftDepth-1 + 2 **rightDepth-1
else:
return 1 + self.countNodes(root.left) + self.countNodes(root.right)