数据结构与算法——Java实现 53.力扣938题——二叉搜索树的范围和

生命的意义

在于活出自我

而不是成为别人眼中的你

------ 24.11.3

938. 二叉搜索树的范围和

给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。

示例 1:

复制代码
输入:root = [10,5,15,3,7,null,18], low = 7, high = 15
输出:32

示例 2:

复制代码
输入:root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
输出:23

提示:

  • 树中节点数目在范围 [1, 2 * 104] 内
  • 1 <= Node.val <= 105
  • 1 <= low <= high <= 105
  • 所有 Node.val 互不相同

方法1 中序遍历判断范围

思路

由题目给出的输入序列可以看出,输入序列按照二叉树的层次遍历顺序进行排序

我们按照中序遍历(左 - 根 - 右)的顺序排列给出的节点,遍历树节点,如果树节点在此范围内,则将该节点的值加入总和中,若不在该范围内,则跳过该节点,观察下一个节点,直到遍历完所有节点为止

返回总和

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 {
    public int rangeSumBST(TreeNode node,int low,int high){
        TreeNode p = node;
        LinkedList<TreeNode> stack = new LinkedList<>();
        int sum = 0;
        while(p != null || !stack.isEmpty()){
            if (p != null || !stack.isEmpty()){
                if (p != null){
                    stack.push(p);
                    // 左
                    p = p.left;
                }else {
                    TreeNode pop = stack.pop();
                    // 处理值
                    if (pop.val >= low && pop.val <= high){
                        sum += pop.val;
                    }
                    // 右
                    p = pop.right;
                }
            }
        }
        return sum;
    }
}

方法2 中序遍历剪枝优化

由于二叉搜索树的特性(左 < 根 < 右),当我们遍历到某一结点时发现该节点不在范围内,则其左子树/右子树可以直接进行剪枝操作

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 {
    public int rangeSumBST(TreeNode node,int low,int high){
        TreeNode p = node;
        LinkedList<TreeNode> stack = new LinkedList<>();
        int sum = 0;
        while(p != null || !stack.isEmpty()){
            if (p != null || !stack.isEmpty()){
                if (p != null){
                    stack.push(p);
                    // 左
                    p = p.left;
                }else {
                    TreeNode pop = stack.pop();
                    // 处理值
                    if (pop.val > high){
                        break;
                    }
                    if (pop.val >= low){
                        sum += pop.val;
                    }
                    // 右
                    p = pop.right;
                }
            }
        }
        return sum;
    }
}

方法3 上下限递归剪枝优化

思路

从根节点开始进行递归,对于每个节点进行判断,若它的val值大于要求的范围,则将其右子树跳过不用遍历判断,若其val值小于要求的范围,则将其左子树跳过不用遍历判断,如此可大大提高程序的效率 (通过范围上下限判定进行剪枝操作)

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 {
    public int rangeSumBST(TreeNode node,int low,int high){
        if (node == null){
            return 0;
        }
        if (node.val < low){
            // 将节点的左子树省去
            return rangeSumBST(node.right,low,high);
        }
        if (node.val > high){
            // 将节点的右子树省去
            return rangeSumBST(node.left,low,high);
        }

        return node.val + rangeSumBST(node.left,low,high) + rangeSumBST(node.right,low,high);
    }
}
相关推荐
Cosolar28 分钟前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
Navigator_Z2 小时前
LeetCode //C - 1318. Minimum Flips to Make a OR b Equal to c
c语言·算法·leetcode
yiangL_2 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
geovindu4 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk81635 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾5 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
驭渊的小故事6 小时前
LeetCode 33:搜索旋转排序数组——从二分查找到旋转数组的区间判定
算法·leetcode·职场和发展
听情歌落俗7 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力7 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
落羽的落羽8 小时前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法