This is a Graph Problem - Not a Tree Problem! Part IV
I think this problem had everything: hashtables, hashsets, recursive DFS and non-recursive DFS and even applications of the dangerous .GetHashCode(), which I don't like but sometimes it is good enough when the constraints are limited, which is the case of this problem. But the problem isn't a tree problem - it is a graph one. Convert the tree to a graph (needed because you'll be navigating up and down the tree), and that's where I'm using .GetHashCode() to uniquely identify the nodes (worked), then for each node (non-recursive DFS) perform a search (recursive DFS) looking for the max path, traversing only in the case of unique numbers. Notice that you need to keep going even if you find a -ve node since there might be a larger unvisited number behind that node. Code is down below, cheers, ACC. Maximum Distinct Path Sum in a Binary Tree - LeetCode You are given the root of a binary tree , where each node contains an integer value. A valid path in the tree is...