Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Tree Maximum Path Sum #43

Open
kokocan12 opened this issue Jun 25, 2022 · 0 comments
Open

Binary Tree Maximum Path Sum #43

kokocan12 opened this issue Jun 25, 2022 · 0 comments

Comments

@kokocan12
Copy link
Owner

kokocan12 commented Jun 25, 2022

Problem

When a root node of binary tree is given, find max-sum of sequence of nodes.
Sequence of nodes means that traverse node to node, but not visit same node.

example
/*
     -100
     /   \
    2     3
   /       \ 
  4         7
*/
Node(7) -> Node(3) is the sequence that has sum of 10.

/*
       1
     /   \
    2     3 
   / \   / \
  4   5 6   7
*/

Node(7) -> Node(3) -> Node(1) -> Node(2) -> Node(5) is the sequence that has sum of 18.

Naive Approach

We can get a single maximum sum path, that start from parent to child.
image

We can also divide this problem to [LEFT-SIDE] + head + [RIGHT-SIDE].
Then, repeat this at every node.

The code is below.

const maxPathSum = function(root) {
    
    let maxSum = Number.NEGATIVE_INFINITY;
    
    function pathSum(root, sum, tempMax = [Number.NEGATIVE_INFINITY]) {
        if(root === null) return tempMax[0];
        
        sum = sum + root.val
        
        tempMax[0] = Math.max(tempMax[0], sum);
        
        pathSum(root.left, sum, tempMax);
        pathSum(root.right, sum, tempMax);
        
        
        return tempMax[0];
    }
    
    function travel(root) {
        if(root === null) return;
        
        const temp = root.val + Math.max(pathSum(root.left, 0), 0) + Math.max(pathSum(root.right, 0), 0);
        
        maxSum = Math.max(temp, maxSum);
        
        travel(root.left);
        travel(root.right);
    }
    
    travel(root);
    
    
    return maxSum;
};

Better Way

The algorithm above has a time complexity of O(n^2), because it visits same node many times.
We can reduce time complexity to O(n) by combining travel and pathSum into one.

The node has two values, one is max path part(the connection part), it will be used as a child node.
The other one is complete part, where the node is the head of the connection.

The code is below.

const maxPathSum = function(root) {
    
    let maxSum = Number.NEGATIVE_INFINITY;
    
    function pathSum(node) {
        if(node === null) return 0;
        
        const val = node.val;
        
        let left = pathSum(node.left);
        let right = pathSum(node.right);
        left = Math.max(left, 0);
        right = Math.max(right, 0);
        
        // It is the complete part.
        maxSum = Math.max((val + left + right), maxSum);
        
        // It is the connection part.
        return val + Math.max(left, right);
    }
    
    pathSum(root);
    
    return maxSum;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant