124. Binary Tree Maximum Path Sum

Source: Internet
Author: User

    /** 124. Binary Tree Maximum Path Sum * 11.21 by Mingyang initially wanted to use only one int * Max to pass the argument, but later found that no matter how to change, the return Max will be appended with the initial value * So here we go. A global variable, where any path has to go through one of the highest points, then the highest point is the node in root, and the maximum value is updated each time.     So node says it must have passed. * Then the returned integer is if the largest path also leads to the maximum value that the parent node can provide.     That is, I can provide an interface to the parent node, * and the maximum value. * Key point 1: Clarify what the return value of the recursive function is: * The return value in this case represents the maximum of the parent that can go to root through the root node, and this value is returned to the calling parent function as the return object * because it cannot be referenced in Java as in C + + or by using a pointer to pass a value, previous method is to use global variables*/    intMaxValue;  Public intmaxpathsum (TreeNode root) {MaxValue=Integer.min_value;        Maxpathdown (root); returnMaxValue; }    Private intMaxpathdown (TreeNode node) {if(node = =NULL)return0; intleft = Math.max (0, Maxpathdown (Node.left)); intright = Math.max (0, Maxpathdown (node.right)); MaxValue= Math.max (MaxValue, left + right +node.val); /** This place may be a little confused, you might think *-100 * 19 14 * This situation MaxValue definitely 19, then why return to the + + (-100)?         ? * That's because maxvalue is what we end up with, and the return is just the equivalent of providing an interface for a higher level * giving them a bigger possibility, in case the top root is 1000, then this interface is used. To get the right results * so all we need here is return a possibility that doesn't affect the maximum result*/        returnMath.max (left, right) +Node.val; }    //then you say I don't use global variables, that's the only array:     Public intmaxPathSum1 (TreeNode root) {int[] max =New int[1]; max[0] =Integer.min_value;        MaxPathSum1 (max, Root); returnMax[0]; }    Private intMAXPATHSUM1 (int[] max, TreeNode root) {        if(Root = =NULL)            return0; intLeftmax = Math.max (0, MAXPATHSUM1 (max, root.left)); intRightmax = Math.max (0, MAXPATHSUM1 (max, root.right)); max[0] = Math.max (max[0], root.val+leftmax+Rightmax); returnroot.val+Math.max (Leftmax,rightmax); }

124. 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.