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.

Examples:

```
Input : Binary tree:
4
/ \
2 5
/ \ / \
7 1 2 3
/
6
Output : 13
```**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

**Input:**

The function takes a single argument as input the reference pointer to the **root** of the binary tree.

There will be **T**, test cases and for each test case the function will be called separately.

**Output:**

For each test case print the required sum on a new line.

**Constraints:**

1<=T<=10^{3}

1<=N<=10^{3}

**Example:
Input:**

2

2

1 2 L 1 3 R

7

4 2 L 4 5 R 2 7 L 2 1 R 1 6 L 5 2 L 5 3 R

4

13

Author: harshitsidhwa

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

geetyadav | 82 |

hari32147 | 76 |

AdityaRaj9 | 75 |

yashrana1 | 74 |

amitpandey19 | 67 |

abducodes | 850 |

harshtyagi36 | 417 |

vamsikrishna55 | 323 |

sandipbhaumik | 321 |

Akhil varma | 314 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.