力扣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);
        }
    }
}
相关推荐
JasonChen@66614 小时前
2.Tomcat 安装部署与一键自动化脚本实战
java·tomcat
开开心心就好14 小时前
显示器关不掉怎么办,用小工具一键息屏
算法·智能手机·pdf·计算机外设·ocr·lua·1024程序员节
专注仿真14 小时前
基于 AFSIM 的智能体训练:多智能体强化学习篇 —— QTRAN 算法实战
开发语言·python·算法·php·强化学习·afsim智能体·仿真训练
FR02EN77714 小时前
[LeetCode] 26、80.删除有序数组中的重复项 题解
数据结构·算法
SimonKing14 小时前
零 OCR 依赖!Firecrawl 这个 Rust 库把 Word / PPT / Excel 全变成 Markdown
java·后端·程序员
Rain的Java大神之路14 小时前
别被 ChatGPT 的打字机效果骗了:一字一字蹦,才是 LLM 自回归的命门(附大厂 Java 流式落地)
java·后端·面试
渣渣盟14 小时前
狂命爆肝99天,共52K字的JAVA学习笔记奉上,JAVA从入门到精通一文搞定,一文在手JAVA无忧
java·笔记·学习
星落梦生16 小时前
Maven高级(9)
java·maven
forest_091 天前
《AI 编程 Harness 实战手册》04|审批这道门:点了一百次“允许”,还剩多少作用?
人工智能·算法·prompt·agi
兴趣互联1 天前
jeecgboot nacos2.3.2 使用pgsql
java