当前位置:   article > 正文

【CT】LeetCode手撕—124. 二叉树中的最大路径和

【CT】LeetCode手撕—124. 二叉树中的最大路径和


题目


1- 思路

模式识别:最大路径和 ——> 递归

递归 dfs 思路

递归公式

  • sum = 自己 + 左 + 右

递归中的返回结果

  • output = 自己 + max(左,右)

找结果——更新结果

  • maxSum = Math.max(maxSum, sum);

2- 实现

⭐124. 二叉树中的最大路径和——题解思路

在这里插入图片描述

class Solution {
    int maxSum = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        // 递归
        dfs(root);
        return maxSum;
    }


    public int dfs(TreeNode root){
        // 1. 终止条件
        if(root==null){
            return 0;
        }

        // 2. 递归公式,递归逻辑
        int left = dfs(root.left);
        int right = dfs(root.right);
        int sum = root.val + left + right;

        maxSum = Math.max(sum,maxSum);

        // 3. 每次返回值
        int output = root.val + Math.max(left,right);
        if(output>0) return output;
        return 0;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

3- ACM 实现

public class treeMaxPath {

    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int x){
            val = x;
        }
    }

    public static TreeNode build(Integer[] nums){
        // 借助队列来构造树
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);
        int index = 1;
        while(!queue.isEmpty()){
            TreeNode node = queue.poll();
            if(index<nums.length && nums[index]!=null){
                node.left = new TreeNode(nums[index]);
                queue.offer(node.left);
            }
            index++;
            if(index<nums.length && nums[index]!=null){
                node.right = new TreeNode(nums[index]);
                queue.offer(node.right);
            }
            index++;
        }
        return root;
    }

    public static int dfs(TreeNode root){
        // 1.终止条件
        if(root==null){
            return 0;
        }

        // 2. 递归
        int left = dfs(root.left);
        int right = dfs(root.right);
        int sum = root.val + left + right;
        maxSum = Math.max(sum,maxSum);

        int output = root.val + Math.max(left,right);
        if(output>0) return output;
        return 0;
    }

    static int maxSum = Integer.MIN_VALUE;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入二叉树构造数组");
        String input = sc.nextLine();
        input = input.replace("[","");
        input = input.replace("]","");
        String[] parts = input.split(",");
        Integer[] nums = new Integer[parts.length];
        for(int i = 0 ; i < parts.length;i++){
            if(!parts[i].equals("null")){
                nums[i] = Integer.parseInt(parts[i]);
            }else{
                nums[i] = null;
            }
        }
        TreeNode root = build(nums);
        dfs(root);
        System.out.println("结果是"+maxSum);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/774076
推荐阅读
相关标签
  

闽ICP备14008679号