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;
    }
}
相关推荐
Camel卡蒙3 小时前
红黑树详细介绍(五大规则、保持平衡操作、Java实现)
java·开发语言·算法
AhriProGramming3 小时前
Flask-SQLAlchemy精读-双语精选文章
python·算法·flask
孤廖3 小时前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
BanyeBirth4 小时前
C++动态规划——LIS(最长不下降子序列)
算法·动态规划
小龙报4 小时前
《算法通关指南---C++编程篇(3)》
开发语言·c++·算法·visualstudio·学习方法·visual studio
凤山老林4 小时前
排序算法:详解插入排序
java·开发语言·后端·算法·排序算法
知星小度S4 小时前
算法训练之多源BFS
算法·宽度优先
2201_758875444 小时前
LeetCode:19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
Wenhao.5 小时前
LeetCode 合并K个升序链表
leetcode·链表·golang