> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2024/03-2024-march-16/20-sum-of-nodes-on-the-longest-path-from-root-to-leaf-node.md).

# 20. Sum of nodes on the longest path from root to leaf node

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/sum-of-the-longest-bloodline-of-a-tree/1)

## My Approach

* Implement Depth First Search (DFS) to traverse the binary tree.
* At each node, calculate the sum and length of the longest path from the root to a leaf node.
* Maintain a pair of integers representing the sum and length of the longest path encountered so far.
* Compare the longest paths from the left and right subtrees. If they have the same length, choose the path with the maximum sum.
* Update the sum and length of the current longest path based on the chosen subtree.
* Recursively compute the sum and length for each subtree.
* Finally, return the sum of nodes along the longest path from the root to any leaf node.

## Time and Auxiliary Space Complexity

* **Time Complexity**: The time complexity of this approach is `O(N)`, where N is the number of nodes in the binary tree.
* **Auxiliary Space Complexity**: The auxiliary space complexity is `O(H)`, where H is the height of the binary tree

## Code (C++)

```cpp
class Solution
{
    public:
    function<pair<int,int>(Node *)>dfs=[&](Node* node)->pair<int,int>
    {
        if(!node)
            return {0, 0};
        pair<int,int>left=dfs(node->left);
        pair<int,int>right=dfs(node->right);
        pair<int,int>ans;
        if(left.second>right.second)
            ans=left;
        else if(right.second>left.second)
            ans=right;
        else
        {
            ans.first=max(left.first, right.first);
            ans.second=left.second;
        }
        ans.first+=(node->data);
        ans.second++;
        return ans;
    };
    
    int sumOfLongRootToLeafPath(Node *root)
    {
        return dfs(root).first;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/03-2024-march-16/20-sum-of-nodes-on-the-longest-path-from-root-to-leaf-node.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
