|
| 1 | +# Sum of the Longest Bloodline of a Tree (Sum of nodes on the longest path from root to leaf node) |
| 2 | +## Easy |
| 3 | +<div class="problems_problem_content__Xm_eO"><p><span style="font-size:20px">Given a binary tree of size <strong>N.</strong> Your task is to complete the function <strong>sumOfLongRootToLeafPath(),</strong> that find the sum of all nodes on the longest path from root to leaf node.<br> |
| 4 | +If two or more paths compete for the longest path, then the path having maximum sum of nodes is being considered.</span></p> |
| 5 | + |
| 6 | +<p><strong><span style="font-size:20px">Example 1:</span></strong></p> |
| 7 | + |
| 8 | +<pre><span style="font-size:18px"><strong>Input:</strong> |
| 9 | + 4 |
| 10 | + / \ |
| 11 | + 2 5 |
| 12 | + / \ / \ |
| 13 | + 7 1 2 3 |
| 14 | + / |
| 15 | + 6 |
| 16 | +<strong>Output:</strong> 13 |
| 17 | +<strong>Explanation:</strong> |
| 18 | + <strong>4</strong> |
| 19 | + / \ |
| 20 | + <strong>2</strong> 5 |
| 21 | + / \ / \ |
| 22 | + 7 <strong>1</strong> 2 3 |
| 23 | + / |
| 24 | + <strong>6</strong> |
| 25 | + |
| 26 | +The highlighted nodes <strong>(4, 2, 1, 6)</strong> above are |
| 27 | +part of the longest root to leaf path having |
| 28 | +sum = (4 + 2 + 1 + 6) = 13</span></pre> |
| 29 | + |
| 30 | +<p><strong><span style="font-size:18px">Example 2:</span></strong></p> |
| 31 | + |
| 32 | +<pre><strong><span style="font-size:18px">Input: </span></strong><span style="font-size:18px"> |
| 33 | + 1 |
| 34 | + / \ |
| 35 | + 2 3 |
| 36 | + / \ / \ |
| 37 | + 4 5 6 7</span> |
| 38 | +<strong><span style="font-size:18px">Output: </span></strong><span style="font-size:18px">11</span> |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><span style="font-size:18px"><strong>Your Task:</strong></span><br> |
| 42 | +<span style="font-size:18px">You don't need to read input or print anything. Your task is to complete the function <strong>sumOfLongRootToLeafPath</strong></span><span style="font-size:18px"><strong>() </strong>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.</span></p> |
| 43 | + |
| 44 | +<p><span style="font-size:18px"><strong>Expected Time Complexity:</strong> O(N)<br> |
| 45 | +<strong>Expected Auxiliary Space:</strong> O(N)</span></p> |
| 46 | + |
| 47 | +<p><span style="font-size:18px"><strong>Constraints:</strong><br> |
| 48 | +1 <= Number of nodes <= 10</span><sup><span style="font-size:15px">4</span></sup><br> |
| 49 | +<span style="font-size:18px">1 <= Data of a node <= 10<sup>4</sup></span></p> |
| 50 | +</div> |
0 commit comments