Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the range and sum of binary search tree by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:23:24 10月04日 Update

Editor to share with you how to achieve the scope of LeetCode binary search tree and, I hope you will learn something after reading this article, let's discuss it together!

Topic description

Given the root node root of the binary search tree, returns the sum of the values of all nodes between L and R (inclusive).

The binary search tree is guaranteed to have unique values.

Example 1:

Input: root = [10, 5, 15, 15, 3, 7, 18], L = 7, R = 15 output: 32

Example 2:

Input: root = [10, 5, 15, 15, 7, 13, 18, 18, 1, 6, R = 10 output: 23

Tip:

The maximum number of nodes in the tree is 10000. The final answer is guaranteed to be less than 2 ^ 31.

-A witty line of thinking-

-A witty line of thinking-

-A witty line of thinking-

The idea of solving the problem

Tags: depth first traversal

Meaning: the literal meaning of this question is difficult to understand. The original intention is to find out the sum of all X > = L and X R, then return the sum of the left subtrees.

Current node X > = L and X R) {return rangeSumBST (root.left, L, R);} return root.val + rangeSumBST (root.left, L, R) + rangeSumBST (root.right, L, R) }} after reading this article, I believe you have a certain understanding of "how to realize the scope of binary search tree in LeetCode". If you want to know more about it, please follow the industry information channel. Thank you for your reading!

Tags: Node search sum subtree wit example node scope article guarantee input output necessity between code meaning size literal finished that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker MySQL Microsoft Redmi