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.
10 / \ / \ 11 9 / \ /\ / \ / \ 13 12 13 8 Maximum Consecutive Path Length is 3 (10, 11, 12) Note: 10, 9 ,8 is NOT considered since the nodes should be in increasing order. 5 / \ / \ 8 11 / \ / \ 9 10 / / / / 6 15 Maximum Consecutive Path Length is 2 (8, 9).
Every node in the Binary Tree can either become part of the path which is starting from one of its parent node or a new path can start from this node itself. The key is to recursively find the path length for the left and right sub tree and then return the maximum. Some cases need to be considered while traversing the tree which are discussed below.
prev : stores the value of the parent node. Initialize prev with one less than value of root node so that the path starting at root can be of length at least 1.
len : Stores the path length which ends at the parent of currently visited node.
Case 1: Value of Current Node is prev +1
In this case increase the path length by 1, and then recursively find the path length for the left and the right sub tree then return the maximum between two lengths.
Case 2: Value of Current Node is NOT prev+1
A new path can start from this node, so recursively find the path length for the left and the right sub tree. The path which ends at the parent node of current node might be greater than the path which starts from this node.So take the maximum of the path which starts from this node and which ends at previous node.
Below is the implementation of above idea.
Maximum Consecutive Increasing Path Length is 3
This article is contributed by Chirag Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum XOR path of a Binary Tree
- Maximum Path Sum in a Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Find the maximum sum leaf to root path in a Binary Tree
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Maximum length cycle that can be formed by joining two nodes of a binary tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Path length having maximum number of bends
- XOR of path between any two nodes in a Binary Tree
- Count of 1's in any path in a Binary Tree
- Longest consecutive sequence in Binary tree
- Print path between any two nodes in a Binary Tree | Set 2
- Minimum sum path between two leaves of a binary tree
- Longest Path with Same Values in a Binary Tree
- Print all Coprime path of a Binary Tree
- Print path between any two nodes in a Binary Tree
- Maximum XOR with given value in the path from root to given node in the tree
- Find all root to leaf path sum of a Binary Tree