Esempio n. 1
0
 int maxPathSum(TreeNode *root, int& maxPath) {
     if (!root) return 0;
     int maxLeft = max(0, maxPathSum(root->left, maxPath));
     int maxRight = max(0, maxPathSum(root->right, maxPath));
     maxPath = std::max(maxPath, maxLeft + maxRight + root->val);
     return max(maxLeft, maxRight) + root->val;
 }
Esempio n. 2
0
 int maxPathSum(TreeNode *root, int &max_path_sum) {
     if (root == NULL) return 0;
     int left = maxPathSum(root->left, max_path_sum);
     int right = maxPathSum(root->right, max_path_sum);
     max_path_sum = max(max_path_sum, left + right + root->val);
     int ret = root->val + max(left, right);
     return ret > 0? ret:0;
 }
 int maxPathSum(TreeNode *root, int &path) {
     if(root == NULL) {
         return INT_MIN;
     }
     int l_path = INT_MIN, r_path = INT_MIN;
     int l_ans = maxPathSum(root->left, l_path);
     int r_ans = maxPathSum(root->right, r_path);
     path = max(max(l_path, r_path), 0) + root->val;
     return max(max(l_ans, r_ans), max(l_path, 0) + max(r_path, 0) + root->val);
 }
    /**
     * @param root: The root of binary tree.
     * @return: An integer
     */
    int maxPathSum(TreeNode *root) {
        // write your code here
		if (NULL == root) {
			return INT_MIN / 10;
		}
		int left = getPath(root->left);
		int right = getPath(root->right);
		int my_max = max(root->val, max(root->val + left, max(root->val + right, root->val + left + right)));
		return max(my_max, max(maxPathSum(root->left), maxPathSum(root->right)));
    }
int maxPathSum(TreeNode* root,int max[]){
    if(root == NULL)    return 0;
    
    int left = maxPathSum(root->left,max);
    int right = maxPathSum(root->right,max);
    
    int cur = maxValue(root->val,maxValue(root->val + left, root->val + right));
    
    max[0] = maxValue(max[0],maxValue(cur,root->val + left + right));
    
    return cur;
}
int maxPathSum(int r, int c)
{
	if( !valid(r, c))
		return 0;

	if (r == n-1 && c == n-1)
		return grid[r][c];	// base

	int path1 = maxPathSum(r, c+1);	// right
	int path2 = maxPathSum(r+1, c);	// down

	return grid[r][c] + max(path1, path2);
}
Esempio n. 7
0
    int maxPathSum(TreeNode* root, int &ma){

    	if(!root)
    		return 0;
    	int left = maxPathSum(root->left,ma);
    	int right = maxPathSum(root->right,ma);

    	int ret = (left>0?left:0) + (right>0?right:0) + root->val;

    	if (ret >ma)
    		ma = ret;
    	return root->val + max(max(left,right),0);

    }
Esempio n. 8
0
int main() {
    char *data = "[1,-2,3]";
    struct TreeNode *root = genBinaryTree(data);
    printf("The max path sum is %d\n", maxPathSum(root));
    freeBinaryTree(root);
    return 0;
}
 void maxPathSum(TreeNode *node, int &cur, int &maxSum) {
     if(node == NULL) {
         cur = 0;
         return;
     }
     int leftSum = 0, rightSum = 0;
     maxPathSum(node->left, leftSum, maxSum);
     maxPathSum(node->right, rightSum, maxSum);
     // check if needs to add the left path or right path
     // cur is the max sum from current node, current node + left subtree, current node + right subtree
     cur = max(node->val, max(node->val+leftSum, node->val+rightSum));
     // only when to update the maxsum, it needs to check if add the both left or right pathes
     // cuz only the highest node can add the both left and right pathes
     // maxSum is the max value from preivous maxSum, cur value and cur value + leftSum + rightSum
     maxSum = max(maxSum, max(cur, node->val+leftSum+rightSum));
 }
 int maxPathSum(TreeNode *root) {
     int max = root->val;
     
     maxPathSum(root, max);
     
     return max;
 }
Esempio n. 11
0
int maxPathSum(TreeNode* root) {
	int max[1];
	max[0] = -1<<30;
	maxPathSum(root,max);

	return max[0];
}
Esempio n. 12
0
 int maxPathSum(TreeNode *root, int &len){
     if(!root) {
         len = 0;
         return INT_MIN;
     }
     int left_path_sum = maxPathSum(root->left, len);// calc the max path sum of the left and get the length
     int left_len = len;
     int right_path_sum = maxPathSum(root->right, len); // calc the max path sum of the right and get the length
     int right_len = len;
     int res = max(left_path_sum, right_path_sum); // get the longer one from left and right
     int tmp = root->val; // tmp is a new path that across the root
     if(left_len > 0) tmp += left_len;
     if(right_len > 0) tmp += right_len;
     len = max(root->val, root->val+max(left_len, right_len));// the sum may be negative
     res = max(res, tmp); // compare the path across the root or not
     return res;
 }
 int maxPathSum(TreeNode *root, int &full_len, int &part_len)
 {
     if(root == 0) 
     {
         full_len = 0; 
         part_len = 0;
         return 0;
     }
     if(!root->left && !root->right)
     {
         full_len = root->val;
         part_len = root->val;
         return root->val;
     }
     int full_l = INT_MIN, full_r = INT_MIN, part_l = INT_MIN, part_r = INT_MIN;
     if(root->left && root->right)
     {
         maxPathSum(root->left, full_l, part_l);
         maxPathSum(root->right, full_r, part_r);
         part_len = max(root->val + max(part_l, part_r), root->val);
         full_len = part_l + part_r + root->val;
         if(full_l > full_len)
             full_len = full_l;
         if(full_r > full_len)
             full_len = full_r;
         if(part_len > full_len)
             full_len = part_len;
         return full_len;
     }
     if(root->left)
     {
         maxPathSum(root->left, full_l, part_l);
         part_len = max(root->val + part_l, root->val);
         full_len = max(part_len, full_l);
         return full_len;
     }
     else
     {
         maxPathSum(root->right, full_r, part_r);
         part_len = max(root->val + part_r, root->val);
         full_len = max(part_len, full_r);
         return full_len;
     }
     
 }
int main() {
    int data[] = { 1, 2, 0, 0, 3, 0, 0};
    int *data_ptr = data;
    TreeNode* root = treeCreate(&data_ptr);
    treeShow(root, 0);
    printf("ans : %d\n", maxPathSum(root));
    system("pause");
    return 0;
}
 int maxPathSum(TreeNode *root) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     int cross = 0, ml = -65536, mr = -65536, res = -65536;
     if(root == NULL) return 0;
     if(root->left != NULL) {
         res = max(res, maxPathSum(root->left));
         ml = mp;
         cross += max(0, ml);
     }
     if(root->right != NULL) {
         res = max(res, maxPathSum(root->right));
         mr = mp;
         cross += max(0, mr);
     }
     res = max(res, root->val + cross);
     mp = max(max(ml, mr), 0) + root->val;
     return res;
 }
 int maxPathSum(TreeNode *root, int &max)
 {
     if (!root)
     {
         return 0;
     }
     
     int left = maxPathSum(root->left, max);
     int right = maxPathSum(root->right, max);
     
     int m = max3(root->val+left, root->val+right, root->val+left+right);
     
     if (m > max)
     {
         max = m;
     }
     
     return max3(root->val, root->val+left, root->val+right);
 }
 int helper(TreeNode* root, bool side) {
     if(root == NULL)
         return 0;
     if(side) {
         if(tm.count(root))
             return tm[root];
         return tm[root] = root->val + max(0, max(helper(root->left, true), helper(root->right, true)));
     } else {
         if(fm.count(root))
             return fm[root];
         if(root->left != NULL && root->right != NULL) {
             int sub_ans = max(maxPathSum(root->left), maxPathSum(root->right));
             return fm[root] = max(sub_ans, helper(root->left, true) + helper(root->right, true) + root->val);
         } else if(root->left != NULL) {
             return fm[root] = maxPathSum(root->left);
         } else if(root->right != NULL) {
             return fm[root] = maxPathSum(root->right);
         }
     }
 }
int main(int argc, char *argv[])
{
	TreeNode t1(1), t2(2), t3(3);

	t1.left = &t2;
	t1.right = &t3;

	maxPathSum(&t1);


	return 0;
}
int main(int argc, char const *argv[])
{
    treenode *n2 = treenode_new(-2);
    treenode *n3 = treenode_new(3);
    treenode *n4 = treenode_new(-4);
    treenode *n1 = treenode_new(-1);

    n2->left = n1;
    n2->right = n3;
    n3->right = n4;
    printf("%d\n", maxPathSum(n2));
    return 0;
}
Esempio n. 20
0
int main(int argc, char *argv[])
{
	if (argc!=3)
		throw std::invalid_argument("wrong argument number");
	std::ifstream fin(argv[1]);
	if(!fin)
	{
		std::cerr<<"file does not exsist"<<std::endl;
		return -1;
	}

	std::vector<int> path;
	int i;
	while(fin>>i)
		path.emplace_back(i);
	std::cout<<maxPathSum(atoi(argv[2]),path)<<std::endl;
}
Esempio n. 21
0
 int maxPathSum(TreeNode *root) {
     // Note: The Solution object is instantiated only once and is reused by each test case.
     int maxPath = -999999;
     maxPathSum(root, maxPath);
     return maxPath;
 }
 int maxPathSum(TreeNode *root) {
     // IMPORTANT: Please reset any member data you declared, as
     // the same Solution instance will be reused for each test case.
     int a1 = 0, a2 = 0;
     return maxPathSum(root, a1, a2);
 }
Esempio n. 23
0
 int maxPathSum(TreeNode *root) {
     int len;
     return maxPathSum(root, len);
 }
 int maxPathSum(TreeNode *root) {
     int path = INT_MIN;
     return maxPathSum(root, path);
 }
Esempio n. 25
0
 int maxPathSum(TreeNode* root) {
     int ma = -10000000;
     maxPathSum(root,ma);
     return ma;
 }
 int maxPathSum(TreeNode *root) {
     int cur = 0, maxSum = INT_MIN;
     maxPathSum(root, cur, maxSum);
     return maxSum;
 }
	void test() {
		TreeNode *root = Help::serialize("5,4,8,11,#,13,4,7,2,#,#,#,1");
		Help::out(root);
		int path = maxPathSum(root);
		cout << path << endl;
	}
Esempio n. 28
0
 int maxPathSum(TreeNode *root) {
     int max_path_sum = INT_MIN;
     maxPathSum(root, max_path_sum); 
     return max_path_sum;
 }