Binary Tree maximum path sum

Source: Internet
Author: User

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

For example:
Given the below binary tree,

       1      /      2   3

 

Return6.

Algorithm: (a bit of Dynamic Planning) First, it is clear that we need to calculate from the bottom up, only consider the current node root, we return the current maximum value of a line from the child node to the top, the node at the top of the line is the child node, and the child node cannot have both left and right branches (that is, the online road cannot have both left and right branches. If there is a branch, after the root returns to the previous node, a chain cannot be formed and a split occurs in the root node). Then we compare the root-> Val,

Root-> Val + leftmax (value returned from the left subtree), root-> Val + rightmax (value returned from the right subtree), where the maximum value is the value returned from the root node, when calculating the return value of the root node, we can calculate the maximum line of the chain with the root node as the top node (which can have forks), as long as the comparison, root-> Val, leftmax + root-> Val, root-> Val + rightmax, leftmax + rightmax + root-> Val. The maximum value is the maximum line of the highest-level node based on root, use this maximum value to compare it with the originally saved maximum result (initialized to int_min). If it is greater than the result, update the result and the final result is the result. The Code is as follows, time complexity O (N ), you only need to traverse one side of the binary tree (post-order traversal ):

 1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     int  result; 13     int maxPathSum(TreeNode *root) {14         result=INT_MIN;15         getMax(root);16         return result;17     }18     int getMax(TreeNode* root)19     {20         if(root==NULL)  return 0;21         int leftMax=getMax(root->left);22         int rightMax=getMax(root->right);23         int tmax=max(max(leftMax+root->val,max(rightMax+root->val,root->val)),leftMax+rightMax+root->val);24         if(tmax>result)  result=tmax;25         int rootmax=max(root->val,max(root->val+leftMax,root->val+rightMax));26         return rootmax;27     }28 };

 

Binary Tree maximum path sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.