Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

思路:所求的maximum path sum有可能会包括一个根节点和两条分别在左右子树中的路径,这里构建一个新的树,每个节点存储的值为所给二叉树对应位置节点向下的最大单条路径(不会在根节点分叉)。然后遍历整个树,找到maximum path sum。

代码中copyTree函数为构建树。遍历树并找到maximum path sum在help函数中实现。

 class Solution {
public:
TreeNode* copyTree(TreeNode* root)
{
if (!root) return NULL;
TreeNode* cur = new TreeNode(root->val);
cur->left = copyTree(root->left);
cur->right = copyTree(root->right);
int largest = ;
if (cur->left) largest = max(largest, cur->left->val);
if (cur->right) largest = max(largest, cur->right->val);
cur->val += largest;
return cur;
}
void help(TreeNode* root, TreeNode* cproot, int& res)
{
if (!root) return;
int localSum = root->val;
if (cproot->left && cproot->left->val > )
localSum += cproot->left->val;
if (cproot->right && cproot->right->val > )
localSum += cproot->right->val;
if (localSum > res) res = localSum;
help(root->left, cproot->left, res);
help(root->right, cproot->right, res);
}
int maxPathSum(TreeNode* root) {
TreeNode* cproot = copyTree(root);
int res = INT_MIN;
help(root, cproot, res);
return res;
}
};

Binary Tree Maximum Path Sum - LeetCode的更多相关文章

  1. Binary Tree Maximum Path Sum leetcode java

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

  2. Binary Tree Maximum Path Sum [leetcode] dp

    a(i):在节点i由于单边路径的最大结束 b(i):在节点i路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->righ ...

  3. [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 ...

  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. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  6. LeetCode: Binary Tree Maximum Path Sum 解题报告

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

  7. 【LeetCode】124. Binary Tree Maximum Path Sum

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

  8. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

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

  9. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

随机推荐

  1. Oracle 学习---- 练习语法 循环( loop end loop; for ;while; if elsif end if )

    /*--set serveroutput on;declare mynum number(3) :=0; tip varchar2(10):='结果是 ';begin mynum:=10+100; d ...

  2. 1106 Lowest Price in Supply Chain (25 分)(树的遍历)

    求叶子结点处能活得最低价格以及能提供最低价格的叶子结点的个数 #include<bits/stdc++.h> using namespace std; ; vector<int> ...

  3. js 回车触发点击事件

    $(document).keyup(function(event){ if(event.keyCode ==13){ $("#submit").trigger("clic ...

  4. 【bzoj1976】[BeiJing2010组队]能量魔方 Cube 网络流最小割

    题目描述 一个n*n*n的立方体,每个位置为0或1.有些位置已经确定,还有一些需要待填入.问最后可以得到的 相邻且填入的数不同的点对 的数目最大. 输入 第一行包含一个数N,表示魔方的大小. 接下来 ...

  5. 让JS帮你决定午餐吃什么吧

    最愁就是每天中午吃什么了,有空就做了个 JavaScript 轮播随机选择.会轮播预先自定义的菜单中,然后点选定的时候确定一款.代码可以查看本页源代码获得,你可以自定义修改菜单数组. 效果演示 准备选 ...

  6. Class-dump

    What is class-dump? This is a command-line utility for examining the Objective-C runtime information ...

  7. vue项目--favicon设置以及动态修改favicon

    最近写公司项目时,动态更新favicon 动态更新之前需要有一个默认的favicon. 目前vue-cli搭建的vue项目里面已经有了一个static文件夹,存放静态文件. favicon图片放到该文 ...

  8. html状态码

    100——客户必须继续发出请求101——客户要求服务器根据请求转换HTTP协议版本 200——交易成功201——提示知道新文件的URL202——接受和处理.但处理未完成203——返回信息不确定或不完整 ...

  9. 为Ubuntu Gnome环境创建桌面快捷方式

    为Ubuntu Gnome环境创建桌面快捷方式 安装gnome-panel包 sudo apt-get install --no-install-recommends gnome-panel 启动终端 ...

  10. 洛谷P1160 队列安排

    题目描述 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1.先将1号同学安排进队列,这时队列中只有他一个人: 2.2-N号同学依次入列,编号为i的同学入列方式为:老师 ...