#include "bits/stdc++.h"
using
namespace
std;
struct
Tree {
int
val;
Tree *left, *right;
};
Tree* newNode(
int
data)
{
Tree* temp =
new
Tree();
temp->val = data;
temp->left = temp->right = NULL;
return
temp;
}
void
dfs(Tree* root,
int
currentDifference,
int
count,
int
& maxLength)
{
if
(root->left) {
int
difference = root->left->val
- root->val;
if
(difference == currentDifference) {
dfs(root->left, currentDifference,
count + 1, maxLength);
maxLength = max(maxLength,
count + 1);
}
else
{
dfs(root->left, difference,
2, maxLength);
}
}
if
(root->right) {
int
difference = root->right->val
- root->val;
if
(difference == currentDifference) {
dfs(root->right, currentDifference,
count + 1, maxLength);
maxLength = max(maxLength,
count + 1);
}
else
{
dfs(root->right, difference,
2, maxLength);
}
}
}
int
maximumLengthAP(Tree* root)
{
if
(root == NULL)
return
0;
if
(root->left == NULL
and root->right == NULL) {
return
1;
}
int
maxLength = 2;
if
(root->left) {
int
difference = root->left->val
- root->val;
dfs(root->left, difference, 2,
maxLength);
}
if
(root->right) {
int
difference = root->right->val
- root->val;
dfs(root->right, difference, 2,
maxLength);
}
return
maxLength;
}
int
main()
{
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;
}