Given a binary tree of size N. Your task is to complete the function sumOfLongRootToLeafPath(), that find the sum of all nodes on the longest path from root to leaf node.
If two or more paths compete for the longest path, then the path having maximum sum of nodes is being considered.
Example 1:
Input: 
        4        
       / \       
      2   5      
     / \ / \     
    7  1 2  3    
      /
     6
Output: 13
Explanation:
        4        
       / \       
      2   5      
     / \ / \     
    7  1 2  3 
      /
     6
The highlighted nodes (4, 2, 1, 6) above are 
part of the longest root to leaf path having
sum = (4 + 2 + 1 + 6) = 13
Example 2:
Input: 
          1
        /   \
       2     3
      / \   / \
     4   5 6   7
Output: 11
Your Task:
You don't need to read input or print anything. Your task is to complete the function sumOfLongRootToLeafPath() which takes root node of the tree as input parameter and returns an integer denoting the sum of the longest root to leaf path of the tree. If the tree is empty, return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= Number of nodes <= 10^{4}
1 <= Data of a node <= 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Tree algorithms

Data structures

Graph traversal

Starter code: #User function Template for python3

'''
class Node:
    def __init__(self,val):
        self.data=val
        self.left=None
        self.right=None
'''
class Solution:
    def sumOfLongRootToLeafPath(self,root):
        #code here

Solutions:
class Solution:

	def SumOfLongRootToLeafPath(self, root, Sum, Len, maxLen, maxSum):
		if not root:
			if maxLen[0] < Len:
				maxLen[0] = Len
				maxSum[0] = Sum
			elif maxLen[0] == Len and maxSum[0] < Sum:
				maxSum[0] = Sum
			return
		self.SumOfLongRootToLeafPath(root.left, Sum + root.data, Len + 1, maxLen, maxSum)
		self.SumOfLongRootToLeafPath(root.right, Sum + root.data, Len + 1, maxLen, maxSum)

	def sumOfLongRootToLeafPath(self, root):
		if not root:
			return 0
		maxSum = [-999999999999]
		maxLen = [0]
		self.SumOfLongRootToLeafPath(root, 0, 0, maxLen, maxSum)
		return maxSum[0]


class Solution:

	def sumOfLongRootToLeafPath(self, root):
		res = [0, 0]
		solution(root, res, 0, 0)
		return res[1]

def solution(root, res, height, sm):
	if root == None:
		if res[0] < height:
			res[0] = height
			res[1] = sm
		elif res[0] == height:
			res[1] = max(res[1], sm)
		return
	sm += root.data
	height += 1
	solution(root.left, res, height, sm)
	solution(root.right, res, height, sm)


class Solution:

	def solve(self, root):
		if root is None:
			return (0, 0)
		(left_h, left_sum) = self.solve(root.left)
		(right_h, right_sum) = self.solve(root.right)
		curr_sum = root.data
		height = 1
		if left_h > right_h:
			curr_sum += left_sum
			height += left_h
		elif right_h > left_h:
			curr_sum += right_sum
			height += right_h
		else:
			curr_sum += max(left_sum, right_sum)
			height += right_h
		return (height, curr_sum)

	def sumOfLongRootToLeafPath(self, root):
		(_, summ) = self.solve(root)
		return summ


class Solution:

	def sumOfLongRootToLeafPath(self, root):
		if not root:
			return 0
		l = 0
		mxlen = 0
		s = 0
		mxsum = 0

		def solve(root, s, mxsum, l, mxlen):
			if not root:
				if l > mxlen:
					mxlen = l
					mxsum = s
				elif l == mxlen:
					mxsum = max(mxsum, s)
				return (mxsum, mxlen)
			s += root.data
			(mxsum, mxlen) = solve(root.left, s, mxsum, l + 1, mxlen)
			(mxsum, mxlen) = solve(root.right, s, mxsum, l + 1, mxlen)
			return (mxsum, mxlen)
		(mxsum, mxlen) = solve(root, s, mxsum, l, mxlen)
		return mxsum


class Solution:

	def solve(self, root, l, s, max_sum, max_len):
		if root is None:
			if l > max_len:
				max_sum = s
				max_len = l
			elif l == max_len:
				max_sum = s
			return (max_len, max_sum)
		s += root.data
		(max_len, max_sum) = self.solve(root.left, l + 1, s, max_sum, max_len)
		(max_len, max_sum) = self.solve(root.right, l + 1, s, max_sum, max_len)
		return (max_len, max_sum)

	def sumOfLongRootToLeafPath(self, root):
		l = 0
		s = 0
		max_sum = 0
		max_len = 0
		(max_len, max_sum) = self.solve(root, l, s, max_sum, max_len)
		return max_sum


class Solution:

	def sumOfLongRootToLeafPath(self, root):
		(a, b) = self.Calculate(root)
		return a

	def Calculate(self, root):
		if not root:
			return (0, 0)
		(leftamount, leftlength) = self.Calculate(root.left)
		(rightamount, rightlength) = self.Calculate(root.right)
		if rightlength > leftlength:
			return (rightamount + root.data, 1 + rightlength)
		elif leftlength > rightlength:
			return (leftamount + root.data, 1 + leftlength)
		return (max(leftamount, rightamount) + root.data, 1 + leftlength)


class Solution:

	def slove(self, root, l, summ, maxi, maxil):
		if root is None:
			if l > maxil[0]:
				maxil[0] = l
				maxi[0] = summ
			elif l == maxil[0] and maxi[0] < summ:
				maxi[0] = summ
			return
		self.slove(root.left, l + 1, summ + root.data, maxi, maxil)
		self.slove(root.right, l + 1, summ + root.data, maxi, maxil)

	def sumOfLongRootToLeafPath(self, root):
		maxi = [-9999999]
		maxil = [0]
		self.slove(root, 0, 0, maxi, maxil)
		return maxi[0]


class Solution:

	def sumOfLongRootToLeafPath(self, root):
		return findSum(root, [0, 0], 0, 0)[0]

def findSum(root, res, l, s):
	if root == None:
		return res
	s += root.data
	if l > res[1]:
		res[1] = l
		res[0] = s
	elif l == res[1]:
		res[0] = max(res[0], s)
	findSum(root.left, res, l + 1, s)
	findSum(root.right, res, l + 1, s)
	return res