• LeetCode OJ


    题目:

    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.

    解题思路:

    采用后序遍历。设当前根节点的左子树的最大和为left_ans,右子树的最大和为right_ans;则ans 是 root->val + left_ans 、 root->val + right_ans 和 root->val + left_ans + right_ans的最大值。

    而以该根节点的子树对父节点的贡献为 root->val + max(left_ans, right_ans);

    代码:

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int post_order(TreeNode *root, int &ans) {
            if (root == NULL) return 0;
    
            int left_ans = 0, right_ans = 0;
            if (root->left) left_ans = max(left_ans, post_order(root->left, ans));
            
            if (root->right) right_ans = max(right_ans, post_order(root->right, ans));
            
            ans = max(ans, root->val + left_ans + right_ans);
            return max(root->val + left_ans, root->val + right_ans);
        }
        int maxPathSum(TreeNode *root) {
            if (root == NULL) return 0;
            
            int ans = root->val;
            post_order(root, ans);
            
            return ans;
        }
    };
  • 相关阅读:
    基于python的socket网络编程
    Python3报错:ModuleNotFoundError: No module named '_bz2'
    机器学习博客网站
    《Linux内核设计与实现》 读书笔记(4)--进程的调度
    k8s 简单入门
    docker 简单入门
    python3 教程
    .toml 文件简介
    编码规范
    python3 基本用法
  • 原文地址:https://www.cnblogs.com/dongguangqing/p/3727795.html
Copyright © 2020-2023  润新知