Hot interview questions (maximum path of binary tree)

A path  is defined as a sequence starting from any node in the tree, connecting along the parent node-child node, and reaching any node. The same node appears at most once in a path sequence   . The path  contains at least one  node and does not necessarily pass through the root node.

The path sum  is the sum of the values ​​of each node in the path.

Given the root node of a binary tree  root , return its  maximum path sum , that is, the maximum sum of node values ​​on all paths.

 Input: root = [1,2,3] Output: 6 Explanation: The optimal path is 2 -> 1 -> 3, and the sum of the paths is 2 + 1 + 3 = 6

 As a difficult question in binary trees, this question is actually not that difficult to solve.

       Consider the general situation of the path defined in the question: a road extends from bottom to top, and then extends downward after reaching a highest node. The highest point node is called an inflection point, but there may not be such a node on the left or right side of the inflection point. We need to consider each As an inflection point, each node node will have a maximum sum of paths with its inflection point, which is obtained by the following formula:

leftRree+rightTree+root.val

       Here leftTree/rightTree, as the left and right nodes of the root, contribute to the path sum, which are respectively the left half path of the inflection point and the right half path sum of the inflection point. The process of solving this problem is to examine the formula when all nodes serve as inflection points. The result of , and finally the maximum value is taken. Obviously, this formula has the characteristics of subsequent traversal of dfs (left, right, middle). The parameter of the dfs() method is the current node root, and returns root as the half-side path sum of the son of the inflection point :

  • The base case is to return 0 when recursing to null.
  • Recursively call dfs to find the leftTree and rightTree of the left and right sons respectively.
  • After getting leftTree/rightTree, use this formula to calculate the maximum path max where the current node is the inflection point.
  • Update global maximum path and max
  • The return value is the contribution value of the current node root as the son of the inflection point, which is as follows:
return root.val+Math.max(leftTree,rightTree);

That is, the root will form a longer half-path to the upper inflection point with the larger of its left and right half-paths.

  • When dfs ends, the value in max is the correct result. 

 Note : Since the nodes in the tree may all be negative, when taking the contribution, if it is a negative number, just take 0, which means that the side path where the node is located does not participate in forming the maximum path.

Source code:

    //维护一个全局变量
    int max=Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
     //对入参进行判断
     if(root==null){
         return 0;
     }
      dfs(root);
      return max;
    }
    public int dfs(TreeNode root){
        //递归结束条件
        if(root==null){
            return 0;
        }
        int leftTree=Math.max(dfs(root.left),0);
        int rightTree=Math.max(dfs(root.right),0);
        int all=leftTree+rightTree+root.val;
        //更新最大值
        max=Math.max(max,all);
        return root.val+Math.max(leftTree,rightTree);
    }

Guess you like

Origin blog.csdn.net/dfdbb6b/article/details/132421538