力扣labuladong——一刷day48

文章介绍了如何使用递归遍历方法解决力扣(LeetCode)中的三个二叉树相关问题:1602找最近右侧节点、437路径总和III和560和为K的子数组,展示了在遍历过程中计算和更新状态的解题思路。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


二叉树的递归分为「遍历」和「分解问题」两种思维模式,这道题需要用到「遍历」的思维模式

一、力扣1602. 找到二叉树中最近的右侧节点

/**
 * 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 {
    int dif = Integer.MAX_VALUE;
    TreeNode res = null;
    int high = -1;
    int flag = -1;
    public TreeNode findNearestRightNode(TreeNode root, TreeNode u) {
        fun(root, u, 1,1);
        return res;
    }
    public void fun(TreeNode root, TreeNode u, int index, int depth){
        if(root == null){
            return;
        }
        if(root == u){
            high = depth;
            flag = index;
        }else if(high == depth){
            if((index - flag) < dif){
                res = root;
                dif = index - flag;
            }
        }
        fun(root.left , u, index * 2, depth + 1);
        fun(root.right, u, index *2 + 1, depth + 1);
    }
}

二、力扣437. 路径总和 III

/**
 * 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 {
    Map<Long,Integer> preSumPath = new HashMap<>();
    int res = 0;
    long targetSum , pathSum ;
    public int pathSum(TreeNode root, int targetSum) {
        if(root == null){
            return 0;
        }
        this.targetSum = targetSum;
        this.pathSum = 0;
        preSumPath.put(0L,1);
        fun(root);
        return res;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        pathSum += root.val;
        res += preSumPath.getOrDefault(pathSum - targetSum,0);
        preSumPath.put(pathSum, preSumPath.getOrDefault(pathSum,0)+1);
        fun(root.left);
        fun(root.right);
        preSumPath.put(pathSum,preSumPath.getOrDefault(pathSum,0)-1);
        pathSum -= root.val;
    }
}

三、力扣560. 和为 K 的子数组

class Solution {
    public int subarraySum(int[] nums, int k) {
        int[] preSum = new int[nums.length+1];
        for(int i = 0; i < nums.length; i ++){
            preSum[i+1] = nums[i] + preSum[i];
        }
        int count = 0;
        for(int low = 0; low < preSum.length - 1; low ++){
            for(int high = low; high < preSum.length-1; high ++){
                if(preSum[high + 1] - preSum[low] == k){
                    count ++;
                }
            }
        }
        return count;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值