104.二叉树的最大深度 |559.n叉树的最大深度
- 深度【后序遍历:左右中】:节点到根节点的距离
- 高度【前序遍历:中左右】:节点到叶子结点的距离
根节点的高度 = 树的最大深度
【递归法】
class solution:
def maxdepth(self, root: treenode) -> int:
return self.getdepth(root)
def getdepth(self, node):
if not node:
return 0
leftheight = self.getdepth(node.left) #左
rightheight = self.getdepth(node.right) #右
height = 1 + max(leftheight, rightheight) #中
return height
【递归法:精简代码】
class solution:
def maxdepth(self, root: treenode) -> int:
if not root:
return 0
return 1 + max(self.maxdepth(root.left), self.maxdepth(root.right))
【层序遍历迭代法】
# 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: TreeNode) -> 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 node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return depth
559.n叉树的最大深度
【递归法】
class Solution:
def maxDepth(self, root: 'Node') -> int:
if not root:
return 0
max_depth = 1
for child in root.children:
max_depth = max(max_depth, self.maxDepth(child) + 1)
return max_depth
【迭代法】
"""
# 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: TreeNode) -> int:
if not root:
return 0
depth = 0
queue = collections.deque([root])
while queue:
depth += 1
for _ in range(len(queue)):
node = queue.popleft()
for child in node.children:
queue.append(child)
return depth
111.二叉树的最小深度
力扣链接
陷阱:最小深度,是根节点到叶子结点的最小距离。
如果有节点,有一个子树为空,另一个不为空,则要去不为空的子树,因为为空的子树,不是叶子结点
【递归法】
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
return 1 + min(leftDepth, rightDepth)
def minDepth(self, root):
return self.getDepth(root)
【递归法:精简版】
class Solution:
def minDepth(self, root):
if root is None:
return 0
if root.left is None and root.right is not None:
return 1 + self.minDepth(root.right)
if root.left is not None and root.right is None:
return 1 + self.minDepth(root.left)
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
【迭代法】
# 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: TreeNode) -> 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: #node是叶子结点
return depth
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return depth
222.完全二叉树的节点个数
力扣链接
完全二叉树只有两种情况,
- 情况一:就是满二叉树【可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。】
- 情况二:最后一层叶子节点没有满。【分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。】
【普通二叉树】
【递归法】时间复杂度:O(n) |空间复杂度:O(log n)
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
【递归法:精简版】
class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root:
return 0
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
【迭代法】时间复杂度:O(n) |空间复杂度:O(n)
import collections
class Solution:
def countNodes(self, root: TreeNode) -> int:
queue = collections.deque()
if root:
queue.append(root)
result = 0
while queue:
for i in range(len(queue)):
node = queue.popleft()
result += 1 #记录节点数量
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return result
==【完全二叉树】==时间复杂度:O(log n × log n)|空间复杂度:O(log n)
class Solution:
def countNodes(self, root: TreeNode) -> int:
#终止条件 1.node为空 或者 2.node为满二叉树,节点个数= 2**深度-1
if not root:
return 0
left = root.left
right = root.right
leftDepth = 0 #这里初始为0是有目的的,为了下面求指数方便
rightDepth = 0
while left: #求左子树深度
left = left.left
leftDepth += 1
while right: #求右子树深度
right = right.right
rightDepth += 1
if leftDepth == rightDepth:
return (2 << leftDepth) - 1 #return 2**(leftDepth+1)-1
#注意向左移动一位(2<<1) 相当于2^2,所以leftDepth初始为0,2<<0,相当于2**1=2,位置没动
return self.countNodes(root.left) + self.countNodes(root.right) + 1