binary-tree-maximum-path-sum(mock)

注意:

// 注意,如果一个类放在另一个类里面,初始化时候会报错 Solution is not a enclosing class// 这是因为如果TreeNode不是static,那么要求先有外部类的实例// 要加上static// 或者放到类的外面
https://leetcode.com/problems/binary-tree-maximum-path-sum/
https://leetcode.com/mockinterview/session/result/xslp8c2/
package com.company;

import java.util.ArrayList;
import java.util.List;

class Solution {

    // 注意,如果放在Solution里面,会报错 Solution is not a enclosing class
    // 这是因为如果TreeNode不是static,那么要求先有外部类的实例
    // 要加上static
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public int maxPathSum(TreeNode root) {
        // 要求至少有一个元素,全是负数情况下不能认为是0
        if (root == null) {
            return 0;
        }
        List<Integer> ret = impl(root);
        return ret.get(1);
    }

    // 多值返回一般放在容器里
    // [0]包含root的单一路径最大值; [1] 最大值;
    // 调用时保证root不会为null
    private List<Integer> impl(TreeNode root) {
        List<Integer> ret = new ArrayList();
        int maxWithRoot = root.val;
        int maxRet = root.val;

        if (root.left != null) {
            List<Integer> left = impl(root.left);
            System.out.printf("Here is left %d, ret: %d, %d\n", root.left.val, left.get(0), left.get(1));
            if (left.get(0) > 0) {
                maxWithRoot = root.val + left.get(0);
            }
            maxRet = maxWithRoot > left.get(1) ? maxWithRoot : left.get(1);

        }
        if (root.right != null) {
            List<Integer> right = impl(root.right);
            int tmp = maxWithRoot;
            if (root.val + right.get(0) > maxWithRoot) {
                maxWithRoot = root.val + right.get(0);
            }
            // 下面这个地方因为考虑不周,导致了一个bug,只考虑了maxWithRoot,没有考虑之前的maxRet
            maxRet = maxWithRoot > maxRet ? maxWithRoot : maxRet;
            maxRet = maxRet > right.get(1) ? maxRet : right.get(1);

            // merge two branch
            if (tmp + right.get(0) > maxRet) {
                maxRet = tmp + right.get(0);
            }
        }

        ret.add(maxWithRoot);
        ret.add(maxRet);
        System.out.printf("Here is node %d, ret: %d, %d\n", root.val, maxWithRoot, maxRet);
        return ret;
    }
}

public class Main {

    public static void main(String[] args) {
        // write your code here
        System.out.println("Hello");

        Solution.TreeNode node1 = new Solution.TreeNode(1);
        Solution.TreeNode node2 = new Solution.TreeNode(2);
        Solution.TreeNode node3 = new Solution.TreeNode(3);
        Solution.TreeNode node4 = new Solution.TreeNode(4);
        Solution.TreeNode node5 = new Solution.TreeNode(5);
        Solution.TreeNode node6 = new Solution.TreeNode(6);
        Solution.TreeNode node7 = new Solution.TreeNode(7);
        Solution.TreeNode node8 = new Solution.TreeNode(8);
        Solution.TreeNode node9 = new Solution.TreeNode(9);
        Solution.TreeNode node10 = new Solution.TreeNode(10);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        node4.left = node8;
        node4.right = node9;
        node5.left = node10;

        Solution solution = new Solution();
        int ret = solution.maxPathSum(node1);
        System.out.printf("Get ret: %d\n", ret);

    }
}
时间: 2024-10-23 18:07:16

binary-tree-maximum-path-sum(mock)的相关文章

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven 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 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

Binary Tree Maximum Path Sum 自底向上求解(重重重)

题目: 链接 解答: 自底向上求解.left_max right_max分别返回了左右子树的最大路径和,如果左右子树最大路径和小于0,那么返回零, 用这个最大路径和和根节点的值相加,来更新最大值,同时, 更新返回该树的最大路径值. 代码: class Solution { public: int max = INT_MIN; int maxPathSum(TreeNode *root) { if (root == NULL) return 0; search(root); return max;

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

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

[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. For example: Given the below binary tree, 1 / 2 3 Return 6. [分析]    需要考虑以上两种情况: 1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径 2 左子树 右子树 和根节点形成最大路径 [代码] /******

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

1.  Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get m

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] 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. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

【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 end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 用递归确定每一个节点作为root时,从root出发的最长的路径 在每一次递归中计算maxPath 1 /** 2 * Def

[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. For example: Given the below binary tree, 1 / 2 3 Return 6. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

[Leetcode][Tree][Binary Tree Maximum Path Sum]

找书中权值和最大的路径,至少包含一个点. 有点类似LCA(最近公共祖先),树的问题关键是如何划分子问题,然后递归求解. 想到了可以返回两种结果,一个是单独路径的最大和,一种是子树的最大和,然后在求解的过程中不断的更新答案. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val

Problem Binary Tree Maximum Path Sum

Problem Description: 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. Solution: 1 public class Solution { 2 public int max; 3 public int max