Given a Binary Tree, the task is to find the length of the longest straight path of the given binary tree.
Straight Path is defined as the path that starts from any node and ends at another node in the tree such that the direction of traversal from the source node to the destination node always remains the same i.e., either left or right, without any change in direction that is left->left ->left or right->right->right direction.
The path shown in green is the longest straight path from 4 to 6 which is of length 2.
The path shown in green is the longest straight path from 5 to 0 which is of length 3.
Approach: The idea is to use the Postorder traversal. Follow the steps below to solve the problem:
- For each node, check the direction of the current Node (either left or right) and check which direction of its child is providing the longest length below it to that node.
- If the direction of the current node and the child giving the longest length is not the same then save the result of that child and pass the length of the other child to its parent.
- Using the above steps find the longest straight path at each node and save the result to print the maximum value among all the straight paths.
- Print the maximum path after the above steps.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Print the longest leaf to leaf path in a Binary tree
- Longest Path with Same Values in a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Maximum Consecutive Increasing Path Length in Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Longest path in an undirected tree
- Check whether a given binary tree is skewed binary tree or not?
- Print path from root to a given node in a binary tree
- Sort the path from root to a given node in a Binary Tree
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- 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
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Binary Tree to Binary Search Tree Conversion using STL set
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.