104. 二叉树的最大深度 - 力扣(Leetcode)
一开始使用global,但是报错如下
NameError: name ‘max_depth’ is not defined
if depth > max_depth: Line 15 in traversal (Solution.py)
traversal(root, 1) Line 22 in maxDepth (Solution.py)
ret = Solution().maxDepth(param_1) Line 44 in _driver (Solution.py)
_driver() Line 55 in (Solution.py)
报错版本的代码如下
# 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: Optional[TreeNode]) -> int:
max_depth = 0
def traversal(cur, depth):
global max_depth
if not cur:
return
if depth > max_depth:
max_depth = depth
if cur.left:
traversal(cur.left, depth+1)
if cur.right:
traversal(cur.right, depth+1)
traversal(root, 1)
return max_depth
后来使用属性变量来得到全局的变量,代码如下
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
self.max_depth = 0
def traversal(cur, depth):
if not cur:
return
if depth > self.max_depth:
self.max_depth = depth
if cur.left:
traversal(cur.left, depth+1)
if cur.right:
traversal(cur.right, depth+1)
traversal(root, 1)
return self.max_depth
GPT优化后的代码如下所示,将深度作为返回值,代码更为精简
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
def traversal(cur, depth):
if not cur:
return depth
left_depth = traversal(cur.left, depth + 1)
right_depth = traversal(cur.right, depth + 1)
return max(left_depth, right_depth)
return traversal(root, 0)
111. 二叉树的最小深度 - 力扣(Leetcode)
一次AC,代码如下
# 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: Optional[TreeNode]) -> int:
if not root:
return 0
self.min_depth = float('inf')
def traversal(cur, depth):
if not cur:
return
if not cur.left and not cur.right:
if self.min_depth > depth:
self.min_depth = depth
return
if cur.left:
traversal(cur.left, depth+1)
if cur.right:
traversal(cur.right, depth+1)
traversal(root, 1)
return self.min_depth
GPT的优化意见如下
- 避免使用类成员变量self.min_depth,而是将其作为参数传递给递归函数traversal。
- 使用更具描述性的函数名,例如find_min_depth。
- 使用内置的min函数来更新最小深度。
- 简化条件语句,使代码更加清晰。
优化代码如下
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
def find_min_depth(node, depth):
if not node.left and not node.right:
return depth
min_depth = float('inf')
if node.left:
min_depth = min(min_depth, find_min_depth(node.left, depth + 1))
if node.right:
min_depth = min(min_depth, find_min_depth(node.right, depth + 1))
return min_depth
return find_min_depth(root, 1)
222. 完全二叉树的节点个数 - 力扣(Leetcode)
先直接前序遍历得到节点数量
# 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 countNodes(self, root: Optional[TreeNode]) -> int:
self.count = 0
def traversal(cur):
if not cur:
return
self.count += 1
traversal(cur.left)
traversal(cur.right)
traversal(root)
return self.count
本题也可以利用完全二叉树的特性,代码如下
class Solution: # 利用完全二叉树特性
def countNodes(self, root: TreeNode) -> int:
if not root: return 0
count = 0
left = root.left; right = root.right
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)
总结
今天主要是求二叉树深度以及节点个数,一方面要学会利用递归遍历,一方面要熟悉一些特殊二叉树的特性
附录
代码随想录算法训练营第十六天 | 104.二叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数_小蛙先森的博客-CSDN博客