前端 二叉树路径总和_二叉树中的最大路径总和

前端 二叉树路径总和

Problem statement:

问题陈述:

Given a binary Tree find the maximum path sum. The path may start and end at any node in the tree.

给定二叉树,找到最大路径总和。 该路径可以在树中的任何节点处开始和结束。

Input:

输入:

The first and the only argument contains a pointer to the root of binary tree.

第一个也是唯一的参数包含一个指向二叉树根的指针。

Output:

输出:

Return an integer representing the maximum sum path.

返回表示最大和路径的整数。

Example with explanation:

带有说明的示例:

    Input: 
      6
    /  \
   3    9


    Output: 
    18
    (3->6->9), since all are positive integer, 
    we should consider all nodes sum.

    Input: 
    -14
    /  \
  -26  -36


    Output: 
    -14
    The path sum is maximum at node (-14).

Solution Approach

解决方法

we will use the following concept.

我们将使用以下概念。

For each node there can be four ways that the maximum sum path goes through the node:

对于每个节点,最大和路径可以通过四种方式通过该节点:

  1. Node only.

    仅节点。

  2. Maximum sum path through Left Child + Node.

    通过左子节点+节点的最大和路径。

  3. Maximum sum path through Right Child + Node.

    通过右子节点+节点的最大和路径。

  4. Maximum sum through Left Child + Node + Max path through Right Child.

    通过左子节点的最大和+节点+通过右子节点的最大路径。

we will initialize the resultant maximum sum variable res with INT_MIN, then we will use an ampersand operator for that variable and keep changing it with the following cases: we keep track of four paths and pick up the max one in the end. An important thing to note is, the root of every subtree need to return maximum path sum such that at most one child of root is involved. This is needed for the parent function call.

我们将使用INT_MIN初始化最终的最大和变量res ,然后对该变量使用&运算符,并在以下情况下对其进行更改:我们跟踪四个路径并最终选择最大路径。 需要注意的重要一点是,每个子树的根都需要返回最大路径总和,以便最多包含一个根子。 这是父函数调用所必需的。

In the below code, this sum is stored in 'temp' and returned by the recursive function.

在下面的代码中,此总和存储在“ temp”中 ,并由递归函数返回。

Standard Node:

标准节点:

stuct Node
{
	int data;
	Node *left,*right;
	Node(int data)
	{
		this->data=data;
		left=NULL;
		right=NULL;
	}
};

We will use the above node for further operation.

我们将使用以上节点进行进一步操作。

Pseudo Code:

伪代码:

maxsum(Node *root,&res){
	// if root is NULL then simply return 0.
	if(root==NULL)
		return 0

	// declare temporary variables.
	int temp,int lh,int rh,ans;   

	// call recursive for left half of the node.
	lh=maxsum(root->left,res)     
	
	// call recursive for right half of the node.
	rh=maxsum(root->right,res)    
	
	// temp variable will store sum of current node 
	// value either by. 
	temp=max(max(lh,rh)+root->data,root->data) 
	
	//considering its own value or by combining the 
	// maximum value from left half or right 
	// half of the node.
	// ans will store the maximum sum node value 
	// considering if the current node
	// is the maximum value by checking left half 
	// and right half and node value sum.
	ans=max(temp,lh+rh+root->data)     
	
	res=max(res,ans)    // res will store maximum sum.
	
	return temp;	    // return temp sum value.
}		

Time Complexity for above code is O(n).

以上代码的时间复杂度为O(n)。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Node //node declaration
    {
    int data;
    Node *left, *right;
    Node(int data)
    {
        this->data = data;
        left = NULL;
        right = NULL;
    }
};

int maxsum(Node* root, int& res) // maxsum function
{
    if (root == NULL) // if root==NULL return 0
        return 0;
    int lh, rh, temp, ans;
    lh = maxsum(root->left, res); //left half recursive call
    rh = maxsum(root->right, res); //right half recursive call
    temp = max(root->data, max(lh, rh) + root->data); // temp sum
    ans = max(temp, lh + rh + root->data);
    res = max(res, ans);
    return temp; //return temp sum
}

int maxsumutility(Node* root) // utility function to give max sum
{
    int res = INT_MIN; //initialise res as minimum value
    maxsum(root, res);
    return res;
}

int main()
{
    Node* root = new Node(10);
    root->left = new Node(12);
    root->right = new Node(-6);
    root->left->left = new Node(14);
    root->left->right = new Node(25);
    root->right->left = new Node(-9);
    root->right->right = new Node(15);
    root->left->left->left = new Node(-2);
    root->left->left->right = new Node(3);

    cout << "Maximum sum: ";
    int finalres = maxsumutility(root);
    cout << finalres << "\n";
    return 0;
}

Output

输出量

Maximum sum: 56


翻译自: https://www.includehelp.com/icp/maximum-path-sum-in-a-binary-tree.aspx

前端 二叉树路径总和

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值