938:二叉搜索树的范围和
This commit is contained in:
parent
3bafe3e458
commit
5e1f5e62db
92
LeetCode/src/main/java/leetcode/editor/cn/RangeSumOfBst.java
Normal file
92
LeetCode/src/main/java/leetcode/editor/cn/RangeSumOfBst.java
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
//给定二叉搜索树的根结点 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 互不相同
|
||||||
|
//
|
||||||
|
// Related Topics 树 深度优先搜索 递归
|
||||||
|
// 👍 178 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
//938:二叉搜索树的范围和
|
||||||
|
public class RangeSumOfBst {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new RangeSumOfBst().new Solution();
|
||||||
|
TreeNode treeNode;
|
||||||
|
//32
|
||||||
|
treeNode = new TreeNode(Arrays.asList(10, 5, 15, 3, 7, null, 18));
|
||||||
|
System.out.println(solution.rangeSumBST(treeNode, 7, 15));
|
||||||
|
//23
|
||||||
|
treeNode = new TreeNode(Arrays.asList(10, 5, 15, 3, 7, 13, 18, 1, null, 6));
|
||||||
|
System.out.println(solution.rangeSumBST(treeNode, 6, 10));
|
||||||
|
//44
|
||||||
|
treeNode = new TreeNode(Arrays.asList(15, 9, 21, 7, 13, 19, 23, 5, null, 11, null, 17));
|
||||||
|
System.out.println(solution.rangeSumBST(treeNode, 21, 23));
|
||||||
|
//63
|
||||||
|
treeNode = new TreeNode(Arrays.asList(18, 9, 27, 6, 15, 24, 30, 3, null, 12, null, 21));
|
||||||
|
System.out.println(solution.rangeSumBST(treeNode, 18, 24));
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 root, int low, int high) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (root.val > high) {
|
||||||
|
return rangeSumBST(root.left, low, high);
|
||||||
|
}
|
||||||
|
if (root.val < low) {
|
||||||
|
return rangeSumBST(root.right, low, high);
|
||||||
|
}
|
||||||
|
return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
29
LeetCode/src/main/java/leetcode/editor/cn/RangeSumOfBst.md
Normal file
29
LeetCode/src/main/java/leetcode/editor/cn/RangeSumOfBst.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<p>给定二叉搜索树的根结点 <code>root</code>,返回值位于范围 <em><code>[low, high]</code></em> 之间的所有结点的值的和。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg" style="width: 400px; height: 222px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [10,5,15,3,7,null,18], low = 7, high = 15
|
||||||
|
<strong>输出:</strong>32
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg" style="width: 400px; height: 335px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
|
||||||
|
<strong>输出:</strong>23
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>树中节点数目在范围 <code>[1, 2 * 10<sup>4</sup>]</code> 内</li>
|
||||||
|
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
|
||||||
|
<li>所有 <code>Node.val</code> <strong>互不相同</strong></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>递归</li></div></div>\n<div><li>👍 189</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user