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;
    }
}
相关推荐
NAGNIP1 小时前
一文搞懂机器学习中的特征降维!
算法·面试
NAGNIP1 小时前
一文搞懂机器学习中的特征构造!
算法·面试
Learn Beyond Limits2 小时前
解构语义:从词向量到神经分类|Decoding Semantics: Word Vectors and Neural Classification
人工智能·算法·机器学习·ai·分类·数据挖掘·nlp
你怎么知道我是队长2 小时前
C语言---typedef
c语言·c++·算法
Qhumaing3 小时前
C++学习:【PTA】数据结构 7-1 实验7-1(最小生成树-Prim算法)
c++·学习·算法
踩坑记录5 小时前
leetcode hot100 3.无重复字符的最长子串 medium 滑动窗口(双指针)
python·leetcode
Z1Jxxx5 小时前
01序列01序列
开发语言·c++·算法
汽车仪器仪表相关领域6 小时前
全自动化精准检测,赋能高效年检——NHD-6108全自动远、近光检测仪项目实战分享
大数据·人工智能·功能测试·算法·安全·自动化·压力测试
Doro再努力7 小时前
【数据结构08】队列实现及练习
数据结构·算法