Given a Binary Tree find the length of the longest path which comprises of nodes with consecutive values in increasing order. Every node is considered as a path of length 1.

Examples:

In below diagram binary tree with longest consecutive path(LCP) are shown :

We can solve above problem recursively. At each node we need information of its parent node, if current node has value one more than its parent node then it makes a consecutive path, at each node we will compare node’s value with its parent value and update the longest consecutive path accordingly.

For getting the value of parent node, we will pass the (node_value + 1) as an argument to the recursive method and compare the node value with this argument value, if satisfies, update the current length of consecutive path otherwise reinitialize current path length by 1.

Please see below code for better understanding :

`// C/C++ program to find longest consecutive ` `// sequence in binary tree ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* A binary tree node has data, pointer to left ` ` ` `child and a pointer to right child */` `struct` `Node ` `{ ` ` ` `int` `data; ` ` ` `Node *left, *right; ` `}; ` ` ` `// A utility function to create a node ` `Node* newNode(` `int` `data) ` `{ ` ` ` `Node* temp = ` `new` `Node; ` ` ` `temp->data = data; ` ` ` `temp->left = temp->right = NULL; ` ` ` `return` `temp; ` `} ` ` ` `// Utility method to return length of longest ` `// consecutive sequence of tree ` `void` `longestConsecutiveUtil(Node* root, ` `int` `curLength, ` ` ` `int` `expected, ` `int` `& res) ` `{ ` ` ` `if` `(root == NULL) ` ` ` `return` `; ` ` ` ` ` `// if root data has one more than its parent ` ` ` `// then increase current length ` ` ` `if` `(root->data == expected) ` ` ` `curLength++; ` ` ` `else` ` ` `curLength = 1; ` ` ` ` ` `// update the maximum by current length ` ` ` `res = max(res, curLength); ` ` ` ` ` `// recursively call left and right subtree with ` ` ` `// expected value 1 more than root data ` ` ` `longestConsecutiveUtil(root->left, curLength, ` ` ` `root->data + 1, res); ` ` ` `longestConsecutiveUtil(root->right, curLength, ` ` ` `root->data + 1, res); ` `} ` ` ` `// method returns length of longest consecutive ` `// sequence rooted at node root ` `int` `longestConsecutive(Node* root) ` `{ ` ` ` `if` `(root == NULL) ` ` ` `return` `0; ` ` ` ` ` `int` `res = 0; ` ` ` ` ` `// call utility method with current length 0 ` ` ` `longestConsecutiveUtil(root, 0, root->data, res); ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `Node* root = newNode(6); ` ` ` `root->right = newNode(9); ` ` ` `root->right->left = newNode(7); ` ` ` `root->right->right = newNode(10); ` ` ` `root->right->right->right = newNode(11); ` ` ` ` ` `printf` `(` `"%d\n"` `, longestConsecutive(root)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3

Also discussed on below link:

**Maximum Consecutive Increasing Path Length in Binary Tree**

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Maximum Consecutive Increasing Path Length in Binary Tree
- Longest Path with Same Values in a Binary Tree
- Print the longest leaf to leaf path in a Binary tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Longest Consecutive Subsequence
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Binary Tree to Binary Search Tree Conversion using STL set
- Check if a binary tree is subtree of another binary tree | Set 1
- Check whether a binary tree is a full binary tree or not
- Check if a binary tree is subtree of another binary tree | Set 2
- Binary Tree to Binary Search Tree Conversion
- Check whether a given binary tree is skewed binary tree or not?