力扣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);
        }
    }
}
相关推荐
哎呦没31 分钟前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
Kalika0-01 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
编程、小哥哥1 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
代码雕刻家1 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
sp_fyf_20241 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
IT学长编程2 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
莹雨潇潇2 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
杨哥带你写代码2 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
小字节,大梦想2 小时前
【C++】二叉搜索树
数据结构·c++
我是哈哈hh3 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝