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

分析
给定一颗二叉树,求其最大路径和。对于二叉树,算法大多可以选择递归解决,此题也不例外。
思路参考
如果只是一个节点,那么当然就是这个节点的值了.
如果这个作为root,那么最长路应该就是..
F(left) + F(right) + val…当然如果left,或者right<0就不用加了的= =
从下往上递归遍历…
如果不这个不是root,那么就不能把left和right加起来了…因为只是一条路…
代码如下:

    /** 
     * Definition for a binary tree node. 
     * struct TreeNode { 
     *     int val; 
     *     TreeNode *left; 
     *     TreeNode *right; 
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
     * }; 
     */  

    class Solution {  
    public:  
        int maxVal = INT_MIN;  
        int maxPathSum(TreeNode* root) {  
            if (root == NULL)  
                return 0;  

            maxSum(root);  
            return maxVal;  
        }  

        /*递归函数*/  
        int maxSum(TreeNode *root)  
        {  
            if (root == NULL)  
                return 0;  

            /*求以root为根的当前子树的最大路径和*/  
            int curVal = root->val;  
            int lmaxSum = maxSum(root->left), rmaxSum = maxSum(root->right);  
            if (lmaxSum > 0)  
                curVal += lmaxSum;  
            if (rmaxSum > 0)  
                curVal += rmaxSum;  

            if (curVal > maxVal)  
                maxVal = curVal;  

            /*返回以当前root为根的子树的最大路径和*/  
            return max(root->val, max(root->val + lmaxSum, root->val + rmaxSum));  
        }  
    };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要用Java实现二叉树最长路径的元素集合,你可以按照以下步骤进行: 1. 创建一个二叉树节点类,包含一个值变量和左右子节点的引用。 ```java class TreeNode { int value; TreeNode left; TreeNode right; public TreeNode(int value) { this.value = value; this.left = null; this.right = null; } } ``` 2. 创建一个最长路径的辅助类,用于保存最长路径的元素集合。 ```java class LongestPath { int length; List<Integer> path; public LongestPath() { this.length = 0; this.path = new ArrayList<>(); } } ``` 3. 创建一个递归方法,用于计算二叉树最长路径。 ```java class BinaryTree { public List<Integer> longestPath(TreeNode root) { LongestPath longest = new LongestPath(); calculateLongest(root, longest); return longest.path; } private int calculateLongest(TreeNode node, LongestPath longest) { if (node == null) { return 0; } int leftLength = calculateLongest(node.left, longest); int rightLength = calculateLongest(node.right, longest); int currentLength = Math.max(leftLength, rightLength) + 1; if (currentLength > longest.length) { longest.length = currentLength; longest.path.clear(); longest.path.add(node.value); } else if (currentLength == longest.length) { longest.path.add(node.value); } return currentLength; } } ``` 4. 创建一个测试类,用于验证结果。 ```java public class Main { public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); root.right.left = new TreeNode(6); root.right.right = new TreeNode(7); BinaryTree binaryTree = new BinaryTree(); List<Integer> longestPath = binaryTree.longestPath(root); System.out.println("最长路径的元素集合为:" + longestPath); } } ``` 这样,你就可以通过调用 `longestPath` 方法来二叉树最长路径的元素集合了。输出结果将会是最长路径的元素集合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值