Binary Tree Maximum Path Sum

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

Return 6.

递归求解。

maxPathSum(root)跟maxPathSum(root.left)和maxPathSum(root.right)之间的关系:

root左子树的maxPath,右子树的maxPath以及根节点之间无法建立直接递归关系。也就是说以下的递推式不成立:

maxPathSum(root) = max{ maxPathSum(root.left), maxPathSum(root.right), maxPathSum(root.left) + maxPathSum(root.right) + root.val }

然而,按照动态规划的思路,root的结果跟其左右子树的结果之间应该是存在递推关系的:

maxPathSum(root) = F( maxPathSum(root.left), maxPathSum(root.right), root )

只是,这个root节点加进来之后如何影响最优解?进一步梳理思路:

F( maxPathSum(root.left), maxPathSum(root.right), root )

/ max{maxPathSum(root.left), maxPathSum(root.right)},                                                                   if root 将不包含在最长路径中

=  {

\ max{maxPathSum(root.left), maxPathSum(root.right), max path sum of the path includes root},    if root 将包含在最长路径中

问题将归结为:求出一条包含root节点的最长路径,并比较该路径的长度与其左右子树的最长路径长度。

所以,在递归过程中,我们需要计算两个值:

1)包含节点在内的最长路径(只可能跟该节点的左子树或者右子树相关);

2)该节点作为根节点的子树的最长路径和;

定义class描述这个递归中间结果:

class max_val {
		int max_path_include_root_half_tree; // 辅助值
		int max_path_sum; // 待求解值

		public void set(int x) {
			max_path_include_root_half_tree = max_path_sum = x;
		}
	}

递归过程:

private void maxPathSum(TreeNode root, max_val max_vs) {
		if (root == null) {
			max_vs.set(-2147483647 >> 2);
			return;
		}

		if (root.left == null && root.right == null) {
			max_vs.set(root.val);
			return;
		}

		max_val left_ = new max_val();
		maxPathSum(root.left, left_);

		max_val right_ = new max_val();
		maxPathSum(root.right, right_);

		int a = left_.max_path_include_root_half_tree + root.val;
		int b = right_.max_path_include_root_half_tree + root.val;
		int c = root.val;//a,b,c中包含root的值,并且最多包含了左子树或者右子树,这类路径可用于组成包含父节点的路径
		int d = a + right_.max_path_include_root_half_tree;
		int f = left_.max_path_sum;
		int g = right_.max_path_sum;

		max_vs.max_path_include_root_half_tree = max(new int[] { a, b, c });// 包含root的最长路径

		max_vs.max_path_sum = max(new int[] { a, b, c, d, f, g });// root作为根节点的树的最长路径和

	}

最终求解:

	public int maxPathSum(TreeNode root) {
		// Start typing your Java solution below
		// DO NOT write main() function
		max_val mv_l = new max_val();
		maxPathSum(root, mv_l);

		return mv_l.max_path_sum;
	}

[leetcode]Binary Tree Maximum Path Sum的更多相关文章

  1. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

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

  2. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

  3. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

  4. 【leetcode】Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  5. 26. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  6. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

  7. leetcode@ [124] Binary Tree Maximum Path Sum (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

  8. LeetCode(124) Binary Tree Maximum Path Sum

    题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...

  9. leetcode 124. Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

随机推荐

  1. Javascript高性能编程-提高Dom访问速度

    在浏览器中对于Dom的操作和普通的脚本的操作处于两个不同的dll中,两个dll的交互是比较耗时的,优化对Dom的操作可以提高脚本的执行速度.下面是对如何优化的一些总结: 将需要多次操作的节点存储在一个 ...

  2. 解决Ionic的ion-slide-box 2条数据渲染问题

    当slider数据列表是动态获取时,如果数据结果只有2条数据时,slider列表会多复制俩个,通过下面的slideChange方法做个判断可以解决这个bug 第一步:添加slideChande方法 & ...

  3. ObjectAnimator属性动画应用demo

    感谢慕课网--eclipse_xu 布局文件:activity_main.xml <FrameLayout xmlns:android="http://schemas.android. ...

  4. grep命令学习

    grep(Globally search a Regular Expression and Print), 全面搜索正则表达式并把行打印出来,是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把 ...

  5. Mac打开关闭隐藏文件功能

    在终端输入: 打开:defaults write com.apple.finder AppleShowAllFiles -bool true 关闭:defaults write com.apple.f ...

  6. tabs左右滚动

    $(function () { //IdivLeft小于0,说明左边还有菜单,菜单总数大于8 //IdivLeft等于0,说明菜单总数小于8 //IdivLeft大于0,说明右边还有菜单,菜单总数大于 ...

  7. request,session,application

    JSP 的3个内置对象request,session,application,其实都有一个作用域,这些对象内部有一个Map成员用于存放数据,比如session对象的setAttribute(key,v ...

  8. ctrl+c,ctrl+d,ctrl+z在linux中意义

    ctrl+c,ctrl+d,ctrl+z在linux中意义   ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.   ctrl+c是强制中断程序的执行.   ctrl+z的是将任务中断 ...

  9. 报表控件NCReport教程:集成NCReport到Qt应用程序中

    NCReport是一款轻量级.快速.多平台.简单易用的基于Qt toolkit的C++编写的报表解决方案,目前主要包括报表渲染库和报表设计器GUI应用程序. 但是好多使用NCReport控件的朋友都不 ...

  10. [moka同学收藏]Yii2.0 rules验证规则

    required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...