#BinaryTree二叉树最大路径和

#BinaryTree二叉树最大路径和

介绍

Binary Tree(二叉树)作为一种入门级数据结构在应用中有重大作用,同时也是许多高级数据结构(平衡树:AVL树,红黑树 etc. 高阶树:B/B+树,high-order tries etc.)的基础。
最大路径和问题为求二叉树中所有路径 (从二叉树中的节点 i 到结点 j,最多经过每个结点一次) 中经过的结点值总和最大的路径。

Input: Non-empty binary tree root node root.

Output: The maximum summation among all paths. The path does not need to be given (It can be seen as another a little bit harder problem, if the path is requested).

Example 1:
Input: [1,2,3] (preorder traversal format)

   1
  / \
 2   3

Output: 6 (path(2,1,3))

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

  -10
  / \
 9  20
    / \
  15   7 

Output: 42 (path(15, 20, 7))

方法

Intuitively, this problem is easy since there are not too many methods on processing the binary tree. The idea is to traverse the binary tree and have a global optima and a local optima on each node. The global optima is the final result, and the local optima is used to sent to its parent and for further use during the traversal.

In order to have a better algorithm, the property of the problem needs to be figured out. There are 2 possibilities connections of a node in the optima path. One is both node’s left and right child nodes are selected. We call it pivot node. This kind of node can only have at most 1 in the optima path since each node can be traversed at most once. It there are two nodes and both of their left and right child nodes are selected in the optima path, a node must be traversed at least wice or more. Then, all other nodes in the middle of the optima path should be only have one of its left or right child node in the optima path. For the first node/last node in the optima path, there will be no children selected.

All nodes in the optima path should satisfy:

  1. If a node is a pivot node, meaning selecting both its left and right child nodes, the local optima would be the summation of its left child node local optima + right child node local optima + node.val.
  2. If a node is not a pivot node, the local optima would be the max one of its left child node local optima and right child node local optima + node.val.

Therefore, local optima of each node should be checked when it is a pivot node, and it is not a pivot node until all nodes are examined (Simply check all possible cases). The global optima would be always pick the larger local optima until all local optimas are computed. In this article, postorder traversal is used to check the local optima of each node. The local optima of each node is the value of the node initially.

算法设计

class Solution {
    int max; //global optima
    public int maxPathSum(TreeNode root) {
        max = root.val; //Init
        traversal(root);
        return max;
    }
    
    private int traversal(TreeNode root){
        if(root == null) // not in the path
            return 0;
        int left = Math.max(0, traversal(root.left)); // left local optima
        int right = Math.max(0, traversal(root.right)); // rightlocal optima
        max = Math.max(max, root.val + left + right); //check global optima
        return Math.max(root.val, Math.max(root.val+left, root.val+right));//goes up and checks parent or end
    }
}

算法分析

Correctness:
Initialization:
The initial global maximum value can be selected in multiple ways. It can be equal to the value of any nodes in the binary tree or the smallest value in the programming language. All of these ways will ensure the global maximum is correct.

  1. If global maximum value is initialized to any value of the nodes in the tree, it will be changed/keep the same since it is updated in each round and there will be a new global optima shows up or the initial value is the optima.
  2. If global maximum value is initialized to the lowest value of the data type in programming language, it will also be updated successfully.
  3. If global maximum value is initialized to an arbitrary value, it may fail to be updated since the arbitrary value might be larger than all values and the optimal maximum value so that the result is wrong.

Negative value exists:
If the child node is negative, we may pick it or not depends on the lower level children nodes. Therefore, the bottom up method is used to avoid checking the same nodes multiple times. In this case, if all nodes have negative value, value of each node will be checked and the greatest one will be picked.

Lemma1:
If a left/right leaf node value is negative, set the local optima of the node to 0 meaning do not pick it to the optima path except it is the only node in the optimal path.
Proof:
Whatever the parent node value is, the negative child node value is always lower the parent node value; therefore, it must not in the optima path except it is the only node is the optima solution.

Complexity:

O(n) where n is the number of nodes in the binary tree. All operations including comparing global optimal, changing local optima and select a path or not on each node takes O(1). Postorder traversal traverse each node exactly once. Therefore the totally computational complexity is O(n). No extra space needed.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值