数据结构与算法——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);
    }
}
相关推荐
Kuo-Teng8 分钟前
LeetCode 73: Set Matrix Zeroes
java·算法·leetcode·职场和发展
初见无风9 分钟前
3.4 Boost库intrusive_ptr智能指针的使用
开发语言·boost
王元_SmallA11 分钟前
服务器公网IP、私网IP、弹性IP是什么?区别与应
java·后端
程序猿202312 分钟前
Python每日一练---第六天:罗马数字转整数
开发语言·python·算法
葵续浅笑31 分钟前
LeetCode - 杨辉三角 / 二叉树的最大深度
java·数据结构·算法·leetcode
装不满的克莱因瓶40 分钟前
【Java架构师】各个微服务之间有哪些调用方式?
java·开发语言·微服务·架构·dubbo·restful·springcloud
杨筱毅1 小时前
【穿越Effective C++】条款13:以对象管理资源——RAII原则的基石
开发语言·c++·effective c++
N 年 后1 小时前
cursor和传统idea的区别是什么?
java·人工智能·intellij-idea
CodeLongBear1 小时前
从Java后端到Python大模型:我的学习转型与规划
java·python·学习
Miraitowa_cheems1 小时前
LeetCode算法日记 - Day 94: 最长的斐波那契子序列的长度
java·数据结构·算法·leetcode·深度优先·动态规划