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 firstname.lastname@example.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.
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.
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Length of longest straight path from a given Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Find the maximum sum leaf to root path in a Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Maximum Path Sum in a Binary Tree
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Maximum XOR path of a Binary Tree
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels
- Maximum length cycle that can be formed by joining two nodes of a binary tree
- Path length having maximum number of bends
- Convert a Generic Tree(N-array Tree) to Binary Tree
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Check whether a binary tree is a full binary tree or not