形成算术级数的二叉树底部的最长路径
给定一棵由N个节点组成的二叉树,任务是找到从任何节点到树底部的最长路径的长度,使得所有节点值形成一个算术级数。
例子:
Input:
Output: 4
Explanation:
From the above given tree, the longest path with node values forming an AP is {6, 9, 12, 15} (Length = 4).
Input:
Output: 2
方法:给定的问题可以通过使用递归和在给定的树上执行 DFS 遍历来解决。这个想法是跟踪当前根节点和下一个后代节点之间的差异,并相应地更新最长路径的长度。请按照以下步骤解决给定的问题:
- 初始化一个变量,比如maxLength为1 ,它存储从任何节点到树底部的路径的最大长度,形成一个算术级数。
- 定义一个函数,比如dfs(root, currentDifference, count, maxLength) ,它以当前根节点、当前差值、形成 AP 的节点数以及得到的最大长度作为参数,并执行以下步骤:
- 如果根的左节点存在,则执行以下步骤:
- 求根节点与其左节点的值之差。
- 如果发现差异为currentDifference ,则将maxLength的值更新为maxLength和(count + 1)的最大值,并递归调用函数dfs(root->left, currentDifference, count + 1, maxLength) 。
- 否则,递归调用dfs(root->left, difference, 2, maxLength) 。
- 如果根的右节点存在,则执行以下步骤:
- 求根节点与其右节点的值之间的差异。
- 如果发现差异为currentDifference ,则将maxLength的值更新为maxLength和(count + 1)的最大值,并递归调用函数dfs(root->right, currentDifference, count + 1, maxLength) 。
- 否则,递归调用dfs(root->left, difference, 2, maxLength) 。
- 如果根的左节点存在,则执行以下步骤:
- 如果给定根节点的左子节点存在,则调用dfs(root->left, difference, 2, maxLength) ,其中差异是根节点与其左节点的值之间的差异。
- 如果给定根节点的右子节点存在,则调用dfs(root->right, difference, 2, maxLength) ,其中差异是根节点与其右节点的值之间的差异。
- 完成上述步骤后,将maxLength的值打印为从任何节点到树底部的路径的结果最大长度,从而形成算术级数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Structure of the Tree Node
struct Tree {
int val;
Tree *left, *right;
};
// Function to create a new node
Tree* newNode(int data)
{
Tree* temp = new Tree();
temp->val = data;
temp->left = temp->right = NULL;
return temp;
}
// Function to perform DFS Traversal
// to find the maximum length of a path
// to a bottom node forming an AP
void dfs(Tree* root, int currentDifference,
int count, int& maxLength)
{
// If the root's left child exists
if (root->left) {
// Calculate the difference
int difference = root->left->val
- root->val;
// If the difference is same
// as the current difference
if (difference == currentDifference) {
dfs(root->left, currentDifference,
count + 1, maxLength);
// Update the maxLength
maxLength = max(maxLength,
count + 1);
}
// Otherwise
else {
dfs(root->left, difference,
2, maxLength);
}
}
// If the root's right child exists
if (root->right) {
// Find the difference
int difference = root->right->val
- root->val;
// If the difference is the same
// as the current difference
if (difference == currentDifference) {
dfs(root->right, currentDifference,
count + 1, maxLength);
// Update the maxLength
maxLength = max(maxLength,
count + 1);
}
// Otherwise
else {
dfs(root->right, difference,
2, maxLength);
}
}
}
// Function to find the maximum length
// of the path from any node to bottom
// of the tree forming an AP
int maximumLengthAP(Tree* root)
{
// Base Cases
if (root == NULL)
return 0;
if (root->left == NULL
and root->right == NULL) {
return 1;
}
// Stores the resultant
// maximum length of the path
int maxLength = 2;
// If the root's left child exists
if (root->left) {
int difference = root->left->val
- root->val;
dfs(root->left, difference, 2,
maxLength);
}
// If the root's right child exists
if (root->right) {
int difference = root->right->val
- root->val;
dfs(root->right, difference, 2,
maxLength);
}
// Return the maximum length obtained
return maxLength;
}
// Driver Code
int main()
{
// Given Tree
Tree* root = newNode(6);
root->right = newNode(9);
root->right->left = newNode(7);
root->right->right = newNode(12);
root->right->right->right = newNode(15);
cout << maximumLengthAP(root);
return 0;
}
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
static int maxLength;
// TreeNode class
static class Node
{
public int val;
public Node left, right;
};
static Node newNode(int key)
{
Node temp = new Node();
temp.val = key;
temp.left = temp.right = null;
return temp;
}
// Function to perform DFS Traversal
// to find the maximum length of a path
// to a bottom node forming an AP
static void dfs(Node root, int currentDifference,
int count)
{
// If the root's left child exists
if (root.left != null)
{
// Calculate the difference
int difference = root.left.val - root.val;
// If the difference is same
// as the current difference
if (difference == currentDifference)
{
dfs(root.left, currentDifference,
count + 1);
// Update the maxLength
maxLength = Math.max(maxLength,
count + 1);
}
// Otherwise
else
{
dfs(root.left, difference, 2);
}
}
// If the root's right child exists
if (root.right != null)
{
// Find the difference
int difference = root.right.val - root.val;
// If the difference is the same
// as the current difference
if (difference == currentDifference)
{
dfs(root.right, currentDifference,
count + 1);
// Update the maxLength
maxLength = Math.max(maxLength,
count + 1);
}
// Otherwise
else
{
dfs(root.right, difference, 2);
}
}
}
// Function to find the maximum length
// of the path from any node to bottom
// of the tree forming an AP
static int maximumLengthAP(Node root)
{
// Base Cases
if (root == null)
return 0;
if (root.left == null &&
root.right == null)
{
return 1;
}
// Stores the resultant
// maximum length of the path
maxLength = 2;
// If the root's left child exists
if (root.left != null)
{
int difference = root.left.val - root.val;
dfs(root.left, difference, 2);
}
// If the root's right child exists
if (root.right != null)
{
int difference = root.right.val - root.val;
dfs(root.right, difference, 2);
}
// Return the maximum length obtained
return maxLength;
}
// Driver code
public static void main(String[] args)
{
// Given Tree
Node root = newNode(6);
root.right = newNode(9);
root.right.left = newNode(7);
root.right.right = newNode(12);
root.right.right.right = newNode(15);
System.out.println(maximumLengthAP(root));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
maxLength = 2
class Node:
# Constructor to set the data of
# the newly created tree node
def __init__(self, key):
self.val = key
self.left = None
self.right = None
# Function to perform DFS Traversal
# to find the maximum length of a path
# to a bottom node forming an AP
def dfs(root, currentDifference, count):
global maxLength
# If the root's left child exists
if (root.left != None):
# Calculate the difference
difference = root.left.val - root.val
# If the difference is same
# as the current difference
if (difference == currentDifference):
dfs(root.left, currentDifference, count + 1)
# Update the maxLength
maxLength = max(maxLength, count + 1)
# Otherwise
else:
dfs(root.left, difference, 2)
# If the root's right child exists
if (root.right != None):
# Find the difference
difference = root.right.val - root.val
# If the difference is the same
# as the current difference
if (difference == currentDifference):
dfs(root.right, currentDifference, count + 1)
# Update the maxLength
maxLength = max(maxLength, count + 1)
# Otherwise
else:
dfs(root.right, difference, 2)
# Function to find the maximum length
# of the path from any node to bottom
# of the tree forming an AP
def maximumLengthAP(root):
global maxLength
# Base Cases
if (root == None):
return 0
if (root.left == None and root.right == None):
return 1
# If the root's left child exists
if (root.left != None):
difference = root.left.val - root.val
dfs(root.left, difference, 2)
# If the root's right child exists
if (root.right != None):
difference = root.right.val - root.val
dfs(root.right, difference, 2)
# Return the maximum length obtained
return maxLength
# Given Tree
root = Node(6)
root.right = Node(9)
root.right.left = Node(7)
root.right.right = Node(12)
root.right.right.right = Node(15)
print(maximumLengthAP(root))
# This code is contributed by decode2207.
C#
// C# program for the above approach
using System;
class GFG{
static int maxLength;
// TreeNode class
class Node
{
public int val;
public Node left, right;
};
static Node newNode(int key)
{
Node temp = new Node();
temp.val = key;
temp.left = temp.right = null;
return temp;
}
// Function to perform DFS Traversal
// to find the maximum length of a path
// to a bottom node forming an AP
static void dfs(Node root, int currentDifference,
int count)
{
// If the root's left child exists
if (root.left != null)
{
// Calculate the difference
int difference = root.left.val - root.val;
// If the difference is same
// as the current difference
if (difference == currentDifference)
{
dfs(root.left, currentDifference,
count + 1);
// Update the maxLength
maxLength = Math.Max(maxLength,
count + 1);
}
// Otherwise
else
{
dfs(root.left, difference, 2);
}
}
// If the root's right child exists
if (root.right != null)
{
// Find the difference
int difference = root.right.val - root.val;
// If the difference is the same
// as the current difference
if (difference == currentDifference)
{
dfs(root.right, currentDifference,
count + 1);
// Update the maxLength
maxLength = Math.Max(maxLength,
count + 1);
}
// Otherwise
else
{
dfs(root.right, difference, 2);
}
}
}
// Function to find the maximum length
// of the path from any node to bottom
// of the tree forming an AP
static int maximumLengthAP(Node root)
{
// Base Cases
if (root == null)
return 0;
if (root.left == null &&
root.right == null)
{
return 1;
}
// Stores the resultant
// maximum length of the path
maxLength = 2;
// If the root's left child exists
if (root.left != null)
{
int difference = root.left.val - root.val;
dfs(root.left, difference, 2);
}
// If the root's right child exists
if (root.right != null)
{
int difference = root.right.val - root.val;
dfs(root.right, difference, 2);
}
// Return the maximum length obtained
return maxLength;
}
// Driver code
public static void Main(String[] args)
{
// Given Tree
Node root = newNode(6);
root.right = newNode(9);
root.right.left = newNode(7);
root.right.right = newNode(12);
root.right.right.right = newNode(15);
Console.WriteLine(maximumLengthAP(root));
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(1)