Skip to main content

LeetCode problem #124. Binary tree Maximum path sum.

Problem Statement:

    Given a tree, task is to find out the path with maximum sum of the elements along the path. 

The path can be anywhere in the tree and need not go along the root and the nodes shouldn't be skipped in the path.

Example 1:

Input: [1,2,3]

       1
      / \
     2   3

Output: 6

Example 2:

Input: [-10,9,20,null,null,15,7]

   -10
   / \
  9  20
    /  \
   15   7

Output: 42

 Approach to the solution:  

  • Approach here is simple, first traverse the tree in post order manner.
  • Keep track of maximum values of left and right.
  • Keep track of maximum values of left side and right side.
  • Compare and keep track of maximum values between maximum side max and sub tree.
  • Keep track of global maximum sum of the path in one variable
  • Compare global maximum sum with the sum which got from the above process.
  • Return that maximum sum.
  • This maximum sum is the maximum path of the sum with the given conditions.

 Solution in C++:

class Solution {
public:
    int maxPathSum(TreeNode* root) {
        int ret = INT_MIN;
        cal(root, ret);
        return ret;
    }
   
    int cal(TreeNode* root, int &ret){
        if(!root)
            return 0;
        int left = cal(root->left, ret);
        int right = cal(root->right, ret);
        int maxlr = max(left, right);       
        int side_max = max(maxlr + root->val, root->val);       
        int subr_tree_max = max(side_max, left + right + root->val);       
        ret = max(ret, subr_tree_max);       
        return side_max;
    }
};

Comments

Popular posts from this blog

Leet Code: Problem #710 Random Pick with Blacklist

Given a blacklist  B containing unique integers from [0, N) , write a function to return a uniform random integer from [0, N) which is NOT  in B . Optimize it such that it minimizes the call to system’s Math.random() . Note: 1 <= N <= 1000000000 0 <= B.length < min(100000, N) [0, N)  does NOT include N. See interval notation . Example 1: Input: ["Solution","pick","pick","pick"] [[1,[]],[],[],[]] Output: [null,0,0,0] Example 2: Input: ["Solution","pick","pick","pick"] [[2,[]],[],[],[]] Output: [null,1,1,1] Example 3: Input: ["Solution","pick","pick","pick"] [[3,[1]],[],[],[]] Output: [null,0,0,2] Example 4: Input: ["Solution","pick","pick","pick"] [[4,[2]],[],[],[]] Output: [null,1,3,1] Explanation of Input Syntax: The input is two lists: the subroutines called and their argume...

Leet Code: Problem #1363 Largest Multiple of Three

Given an integer array of digits , return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros. Solution in C++:

Creating Self Signed SSL Certificates for HTTPS Communication

Self Signed CA: Create Private Key for Self Signed CA openssl ecparam -genkey -name secp256r1 | openssl ec -out ca.key     Create CA Certificate for Self Signed CA openssl req -new -x509 -days 36500 -key ca.key -out ca.pem -subj "/C=IN/ST=Karnataka/L=Bengaluru/O=company name/OU=Prod Operations Department/CN=prodops .domain.com   Verify the content of CA certificate openssl x509 -in ca.pem -noout -text Client CERTIFICATE: CLIENT_ID="<Client-Product>" e.g. CLIENT_ID="ClientID" CLIENT_SERIAL="<Client-Release-Number>" e.g. CLIENT_SERIAL="6889" Create Private Key for Client openssl ecparam -genkey -name secp256r1 | openssl ec -out  ${CLIENT_ID}_${CLIENT_SERIAL}.key                   Generate the Certificate Signing Request CSR openssl req -new -key ${CLIENT_ID}_${CLIENT_SERIAL}.key -out ${CLIENT_ID}_${CLIENT_SERIAL}.csr -subj "/C=IN/ST=Karnataka/L=Bengalur...