You are given the root of a binary tree, where each node contains an integer value.
A valid path in the tree is a sequence of connected nodes such that:
The path can start and end at any node in the tree.
The path does not need to pass through the root.
All node values along the path are distinct.
Return an integer denoting the maximum possible sum of node values among all valid paths.
Β
Example 1:
Input:root = [2,2,1]
Output:3
Explanation:
The path 2 β 2 is invalid because the value 2 is not distinct.
The maximum-sum valid path is 2 β 1, with a sum = 2 + 1 = 3.
Example 2:
Input:root = [1,-2,5,null,null,3,5]
Output:9
Explanation:
The path 3 β 5 β 5 is invalid due to duplicate value 5.
The maximum-sum valid path is 1 β 5 β 3, with a sum = 1 + 5 + 3 = 9.
Example 3:
βββββββ
Input:root = [4,6,6,null,null,null,9]
Output:19
Explanation:
The path 6 β 4 β 6 β 9 is invalid because the value 6 appears more than once.
The maximum-sum valid path is 4 β 6 β 9, with a sum = 4 + 6 + 9 = 19.
Β
Constraints:
The number of nodes in the tree is in the range [1, 1000].
-1000 <= Node.val <= 1000βββββββ
Solutions
Solution 1: DFS + Hash Table
We can treat the tree as an undirected graph, using a hash table \(g\) to store the adjacent nodes of each node, where \(g[node]\) contains the parent node, left child node, and right child node of \(node\).
We use depth-first search to traverse the tree and build the hash table \(g\). For each node, we add its parent node, left child node, and right child node to \(g[node]\).
Next, we use another depth-first search to compute the maximum path sum starting from each node. During this process, we use a hash set \(vis\) to record the node values already visited on the current path, ensuring all node values along the path are distinct. For each node, we first check whether it is already in \(vis\); if so, we return \(0\). Otherwise, we add the node value to \(vis\) and compute the path sum starting from that node. We traverse the adjacent nodes in \(g[node]\), recursively compute the path sum starting from each adjacent node, and update the current best. Finally, we remove the current node value from \(vis\) and return the current node value plus the best path sum.
We perform the above computation for every node in the tree and track the maximum path sum. The final answer is the maximum path sum.
The time complexity is \(O(n^2)\), and the space complexity is \(O(n)\), where \(n\) is the number of nodes in the tree.
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcmaxSum(root*TreeNode)int{g:=map[*TreeNode][]*TreeNode{}vardfsfunc(node,p*TreeNode)dfs=func(node,p*TreeNode){ifnode==nil{return}g[node]=append(g[node],p,node.Left,node.Right)dfs(node.Left,node)dfs(node.Right,node)}vis:=map[int]bool{}vardfs2func(node*TreeNode)intdfs2=func(node*TreeNode)int{ifnode==nil||vis[node.Val]{return0}vis[node.Val]=trueres:=node.Valbest:=0for_,nxt:=rangeg[node]{ifv:=dfs2(nxt);v>best{best=v}}vis[node.Val]=falsereturnres+best}dfs(root,nil)ans:=math.MinIntfornode:=rangeg{ans=max(ans,dfs2(node))clear(vis)}returnans}