返回
长长的同一节点值路径——二叉树中的最大单调路径
前端
2023-12-24 22:15:48
在计算机科学中,二叉树是一种数据结构,它将每个节点最多分为两个子树,左子树和右子树。我们把一个顶点作为根,从根节点到叶子的路径称为根叶路径。我们这里所说的单调路径是指,从根节点到叶子的路径上,每个节点的值都相同,这个路径称为单调路径。
题目要求:
给定一个二叉树,要求找出其中最长的单调路径,这个路径可以经过或不经过根节点。
方法:
我们可以用深度优先搜索的方法来解决这个问题,从每个节点开始向下探索,如果某个节点的值与父节点的值相同,那么我们可以继续向下探索,直到遇到不同的值或者遇到叶节点。在探索的过程中,我们记录下当前路径的长度,并与之前记录的最长路径长度进行比较,如果当前路径长度大于之前记录的最长路径长度,那么我们更新最长路径长度。
我们也可以使用递归的方法来解决这个问题,递归函数的参数是一个节点,递归函数首先检查当前节点的值是否与父节点的值相同,如果相同,那么递归函数继续向下探索,如果不同,那么递归函数返回当前路径的长度。在递归函数返回的时候,我们比较当前路径长度与之前记录的最长路径长度,如果当前路径长度大于之前记录的最长路径长度,那么我们更新最长路径长度。
代码示例:
def longest_monotonic_path(root):
"""
Finds the length of the longest path in a binary tree where each node in the path has the same value.
Args:
root: The root node of the binary tree.
Returns:
The length of the longest path.
"""
# Initialize the longest path length to 0.
longest_path = 0
# Call the recursive function to find the longest path.
longest_path = _longest_monotonic_path(root, longest_path)
# Return the longest path length.
return longest_path
def _longest_monotonic_path(node, longest_path):
"""
Finds the length of the longest path in a binary tree where each node in the path has the same value.
Args:
node: The current node in the binary tree.
longest_path: The length of the longest path found so far.
Returns:
The length of the longest path.
"""
# Check if the current node is None.
if node is None:
return 0
# Check if the current node is a leaf node.
if node.left is None and node.right is None:
return 1
# Check if the current node is the root node.
if node is root:
# Check if the left and right subtrees have the same value.
if node.left is not None and node.left.val == node.val:
# Calculate the length of the longest path in the left subtree.
left_path = _longest_monotonic_path(node.left, longest_path)
# Calculate the length of the longest path in the right subtree.
right_path = _longest_monotonic_path(node.right, longest_path)
# Update the longest path length.
longest_path = max(longest_path, left_path + right_path + 1)
# Check if the left and right subtrees have different values.
if node.left is not None and node.left.val != node.val:
# Calculate the length of the longest path in the left subtree.
left_path = _longest_monotonic_path(node.left, longest_path)
# Update the longest path length.
longest_path = max(longest_path, left_path)
# Check if the left and right subtrees have different values.
if node.right is not None and node.right.val != node.val:
# Calculate the length of the longest path in the right subtree.
right_path = _longest_monotonic_path(node.right, longest_path)
# Update the longest path length.
longest_path = max(longest_path, right_path)
# Return the longest path length.
return longest_path
# Check if the left and right subtrees have the same value.
if node.left is not None and node.left.val == node.val:
# Calculate the length of the longest path in the left subtree.
left_path = _longest_monotonic_path(node.left, longest_path)
# Update the longest path length.
longest_path = max(longest_path, left_path + 1)
# Check if the left and right subtrees have different values.
if node.left is not None and node.left.val != node.val:
# Calculate the length of the longest path in the left subtree.
left_path = _longest_monotonic_path(node.left, longest_path)
# Update the longest path length.
longest_path = max(longest_path, left_path)
# Check if the left and right subtrees have different values.
if node.right is not None and node.right.val != node.val:
# Calculate the length of the longest path in the right subtree.
right_path = _longest_monotonic_path(node.right, longest_path)
# Update the longest path length.
longest_path = max(longest_path, right_path)
# Return the longest path length.
return longest_path