力扣labuladong——一刷day56

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

文章目录

  • 前言
  • [一、力扣113. 路径总和 II](#一、力扣113. 路径总和 II)
  • [二、力扣1430. 判断给定的序列是否是二叉树从根到叶的路径](#二、力扣1430. 判断给定的序列是否是二叉树从根到叶的路径)
  • [三、力扣897. 递增顺序搜索树](#三、力扣897. 递增顺序搜索树)
  • [四、力扣938. 二叉搜索树的范围和](#四、力扣938. 二叉搜索树的范围和)

前言


二叉树的递归分为「遍历」和「分解问题」两种思维模式,这道题可以同时用到两种思维模式。 「遍历」的话很简单,你对 BST 做中序遍历,其结果就是有序的,重新构造出题目要求的这个类似链表的二叉树即可。

一、力扣113. 路径总和 II

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 {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null){
            return new ArrayList<>();
        }
        fun(root,targetSum,0);
        return res;
    }
    public void fun(TreeNode root,int targetSum, int sum){
        if(root == null){
            return;
        }
        path.add(root.val);
        if(root.left == null && root.right == null){
            if(sum + root.val == targetSum){
                res.add(new ArrayList<>(path));
            }
            path.remove(path.size()-1);
            return;
        }
        fun(root.left,targetSum,sum+root.val);
        fun(root.right,targetSum,sum+root.val);
        path.remove(path.size()-1);
    }
}

二、力扣1430. 判断给定的序列是否是二叉树从根到叶的路径

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 {
    int len = 0;
    boolean flag = false;
    public boolean isValidSequence(TreeNode root, int[] arr) {
        len = arr.length-1;
        fun(root,arr,0);
        return flag;
    }
    public void fun(TreeNode root, int[] arr , int index){
        if(root == null || index > len){
            return;
        }
        if(root.val == arr[index]){
            if(index == len && root.left == null && root.right == null){
                flag = true;
            }
            fun(root.left,arr,index+1);
            fun(root.right,arr,index+1);
        }
        return;
    }
}

三、力扣897. 递增顺序搜索树

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 {
    TreeNode pre = null, first = null;
    boolean flag = true;
    public TreeNode increasingBST(TreeNode root) {
        fun(root);
        pre.left = null;
        pre.right = null;
        return first;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        fun(root.left);
        if(pre != null){
            pre.right = root;
            pre.left = null;
        }
        pre = root;
        if(flag){
            first = root;
            flag = false;
        }
        fun(root.right);
    }
}

四、力扣938. 二叉搜索树的范围和

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 {
    int sum = 0;
    public int rangeSumBST(TreeNode root, int low, int high) {
        fun(root,low,high);
        return sum;
    }
    public void fun(TreeNode root, int low, int high){
        if(root == null){
            return ;
        }
        if(root.val > high){
            fun(root.left,low,high);
        }else if(root.val < low){
            fun(root.right,low,high);
        }else{
            sum += root.val;
            fun(root.left,low,high);
            fun(root.right,low,high);
        }
    }
}
相关推荐
乐观的Terry几秒前
3、数据库设计与领域实体
java·数据库·spring boot·spring cloud·ai编程
卡提西亚13 分钟前
leetcode-1438. 绝对差不超过限制的最长连续子数组
算法·leetcode·职场和发展
Java面试题总结25 分钟前
LeetCode 93.复原IP地址
算法·leetcode·职场和发展·.net
C++、Java和Python的菜鸟40 分钟前
第4章 后端Web基础(基础知识)
java·开发语言
芷栀夏1 小时前
Java教育平台实战复盘:课程、考试与学习行为分析系统设计
java·开发语言·学习
从零开始的代码生活_1 小时前
C++ 多态详解:虚函数、动态绑定、抽象类与虚表原理
开发语言·c++·后端·学习·算法
yuannl101 小时前
图的存储方式
数据结构
维基框架1 小时前
维基框架(Wiki-Framework) 1.2.0:Mybatis 升级为主从读写分离
java·后端·架构
Memory_荒年1 小时前
Java函数式接口:把代码变成“拼乐高”的快乐,你体验过吗?
java
Memory_荒年1 小时前
WebSocket:让服务器学会“主动搭讪”的黑科技
java