LeetCode 刷题【124. 二叉树中的最大路径和、125. 验证回文串】

124. 二叉树中的最大路径和

自己做

解:后序遍历

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private int max = Integer.MIN_VALUE;

    public int postTravel(TreeNode root){
        if(root == null)
            return 0;
  
        int left = postTravel(root.left);
        int right = postTravel(root.right);

        //选其中一条路径
        if(left + root.val > max)           //选左边
            max = left + root.val;
        if(right + root.val > max)          //选右边
            max = right + root.val;
        if(root.val > max)                  //都不选
            max = root.val;
        //根连接两边路径
        if(left + root.val + right > max)
            max = left + root.val + right;
        
        //向上传递下面的最大路径和
        return Integer.max(Integer.max(left, right) + root.val, root.val);
    }

    public int maxPathSum(TreeNode root) {
        postTravel(root);
        return max;
    }
}

看题解

官方题解

原来只管往上传就好了

java 复制代码
class Solution {
    int maxSum = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        maxGain(root);
        return maxSum;
    }

    public int maxGain(TreeNode node) {
        if (node == null) {
            return 0;
        }
        
        // 递归计算左右子节点的最大贡献值
        // 只有在最大贡献值大于 0 时,才会选取对应子节点
        int leftGain = Math.max(maxGain(node.left), 0);
        int rightGain = Math.max(maxGain(node.right), 0);

        // 节点的最大路径和取决于该节点的值与该节点的左右子节点的最大贡献值
        int priceNewpath = node.val + leftGain + rightGain;

        // 更新答案
        maxSum = Math.max(maxSum, priceNewpath);

        // 返回节点的最大贡献值
        return node.val + Math.max(leftGain, rightGain);
    }
}

125. 验证回文串

自己做

解:头尾双指针

java 复制代码
class Solution {
    public boolean isPalindrome(String s) {
        int head = 0, tail = s.length() - 1;

        //跳过非字符、非数字
        while(head < tail && !(s.charAt(head) >= 'a' && s.charAt(head) <= 'z' ||
                s.charAt(head) >= 'A' && s.charAt(head) <= 'Z' ||
                s.charAt(head) >= '0' && s.charAt(head) <= '9'))
            head++;
        while(head < tail && !(s.charAt(tail) >= 'a' && s.charAt(tail) <= 'z' ||
                s.charAt(tail) >= 'A' && s.charAt(tail) <= 'Z' ||
                s.charAt(tail) >= '0' && s.charAt(tail) <= '9'))
            tail--;

        while(head < tail){
            //统一转为大写比较
            char left = s.charAt(head);
            char right = s.charAt(tail);
            if(left >= 'a')
                left -= 32;
            if(right >= 'a')
                right -= 32;

            if(left != right)      //不匹配
                return false;

            head++;
            tail--;
            //跳过非字符、非数字
            while(head < tail && !(s.charAt(head) >= 'a' && s.charAt(head) <= 'z' ||
                    s.charAt(head) >= 'A' && s.charAt(head) <= 'Z' ||
                    s.charAt(head) >= '0' && s.charAt(head) <= '9'))
                head++;
            while(head < tail && !(s.charAt(tail) >= 'a' && s.charAt(tail) <= 'z' ||
                    s.charAt(tail) >= 'A' && s.charAt(tail) <= 'Z' ||
                    s.charAt(tail) >= '0' && s.charAt(tail) <= '9'))
                tail--;
        }

        return true;
    }
}
相关推荐
超级码力6666 小时前
【Latex文件架构】Latex文件架构模板
算法·数学建模·信息可视化
穿条秋裤到处跑6 小时前
每日一道leetcode(2026.04.29):二维网格图中探测环
算法·leetcode·职场和发展
Merlos_wind6 小时前
HashMap详解
算法·哈希算法·散列表
汉克老师7 小时前
GESP2025年3月认证C++五级( 第三部分编程题(1、平均分配))
c++·算法·贪心算法·排序·gesp5级·gesp五级
Yzzz-F9 小时前
Problem - 2205D - Codeforces
算法
智者知已应修善业10 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
Halo_tjn10 小时前
Java Set集合相关知识点
java·开发语言·算法
生成论实验室11 小时前
《事件关系阴阳博弈动力学:识势应势之道》第四篇:降U动力学——认知确定度的自驱演化
人工智能·科技·神经网络·算法·架构
AI科技星11 小时前
全域数学·72分册:场计算机卷【乖乖数学】
算法·机器学习·数学建模·数据挖掘·量子计算
科研前沿12 小时前
镜像孪生VS视频孪生核心技术产品核心优势
大数据·人工智能·算法·重构·空间计算