Longest Univalue Path - Complete Solution Guide
Longest Univalue Path is LeetCode problem 687, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given the root of a binary tree, return the length of the longest path, where each node in the path has the same value . This path may or may not pass through the root. The length of the path between two nodes is represented by the number of edges between them. Example 1: Input: root = [5,4,5,1,1,null,5] Output: 2 Explanation: The shown image shows that the longest path of the same value (i.e. 5). Example 2: Input: root = [1,4,5,4,4,null,5] Output: 2 Explanation: The shown image shows that the l
Detailed Explanation
The problem asks us to find the length of the longest path in a binary tree where all the nodes in the path have the same value. The path may or may not pass through the root. The length of a path is defined as the number of edges between the nodes.
Solution Approach
The provided solution uses a recursive Depth-First Search (DFS) approach. The `dfs` function calculates the length of the longest univalue path that starts at a given node and extends downwards. It recursively computes the longest univalue paths from the left and right children. If the left (or right) child has the same value as the current node, we extend the path; otherwise, we start a new path. The global variable `ans` keeps track of the longest univalue path found so far, irrespective of whether it includes the current node.
Step-by-Step Algorithm
- Step 1: Initialize a global variable `ans` to 0. This variable will store the length of the longest univalue path found so far.
- Step 2: Define a recursive function `dfs(node)` that takes a node as input.
- Step 3: Base Case: If the node is null, return 0.
- Step 4: Recursively call `dfs` on the left and right children of the node to get the lengths of the longest univalue paths starting at those children (left_length and right_length).
- Step 5: Initialize `left_arrow` and `right_arrow` to 0. These variables will store the lengths of the longest univalue paths extending from the current node to the left and right, respectively.
- Step 6: If the left child exists and has the same value as the current node, set `left_arrow` to `left_length + 1`. Otherwise, `left_arrow` remains 0, indicating no extension to the left.
- Step 7: Similarly, if the right child exists and has the same value as the current node, set `right_arrow` to `right_length + 1`. Otherwise, `right_arrow` remains 0, indicating no extension to the right.
- Step 8: Update the global variable `ans` to the maximum of its current value and the sum of `left_arrow` and `right_arrow`. This step ensures that we keep track of the longest univalue path found so far, even if it passes through the current node.
- Step 9: Return the maximum of `left_arrow` and `right_arrow`. This value represents the length of the longest univalue path that starts at the current node and extends downwards to either the left or the right.
- Step 10: Call `dfs(root)` to start the traversal from the root of the tree.
- Step 11: Return the value of `ans`, which represents the length of the longest univalue path in the tree.
Key Insights
- Insight 1: The longest univalue path at a node can either be contained within the left subtree, the right subtree, or pass through the current node.
- Insight 2: We can use Depth-First Search (DFS) to traverse the tree and calculate the length of the univalue path at each node.
- Insight 3: The path length is measured by the number of *edges* between nodes, which is one less than the number of nodes if counting nodes in the path. The code returns the edge count.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(h)
Topics
This problem involves: Tree, Depth-First Search, Binary Tree.
Companies
Asked at: Snowflake, Sprinklr, Zepto.